0

I am new to c programming using command line arguments so despite finding few simmilar posts I am still stuck on this problem.

I need to pass a command line option that have more than 1 argument, for example

./testProgram --command arg1 arg2 arg3

here is my code

 static struct option long_options[] = {
        {"rdonly",     required_argument, 0,   'a'},
        {"wronly",     required_argument, 0,   'b'},
        {"command",    required_argument, 0,   'c'},
        {"verbose",    no_argument,       0,   'd'},
        {0,0,0,0}
      };


 c=getopt_long(argc,argv, "", long_options, &option_index);

      if(c == -1)
        break;

      switch(c) {
      case 'a':
          rdonly(fd,optarg);
        break;

      case 'b':
        wronly(fd,optarg);
        break;

      case 'c':
        command(optind,optarg,argc,argv);
        break;

      case 'd':
        printf("its verbose\n");
        break;

      default:
        printf("WRONG OPTION\n");
      }
    }


//command function

int command(int optind, char *optarg, int argc, char* argv)
{

  printf("Its option command with argument %s\n",optarg);

        return 0;
}
Saik
  • 993
  • 1
  • 16
  • 40
  • Is the number of arguments for the `--command` option fixed? – fuz Jan 13 '16 at 11:04
  • Lets say it is not. I feel like it can work with fixed number of arguments, but it might cause some troubles later on. – Saik Jan 13 '16 at 11:06
  • If the number of arguments is unbounded, how do you tell an argument to `--command` from the next option or operand? – fuz Jan 13 '16 at 11:07
  • Not entirely sure, I was thinking since the input will look something like this ./testProgram --option1 a --command z x c v --option2 b there must be a way to get the arguments of command option since they do not start with -- and the next thing after arguments start with -- . – Saik Jan 13 '16 at 11:11
  • Are you sure they can't start with `--`? It might be better to make `--command` so it has a terminator. Also, what happens if operands (non-options) follow `--command`? – fuz Jan 13 '16 at 11:12
  • Does your program take operands in addition to options? If it doesn't, you can just put the arguments to `--command` as operands to the program. – fuz Jan 13 '16 at 11:17
  • Not sure I understood what you said. My options( such as command and so on) will always start with --(like --command --pipe and so on) their arguments however do not have dashes in front. so when non-options follow --command they are the arguments for that option. So yes, my program takes operands in addition to options. – Saik Jan 13 '16 at 11:18
  • “operands” are non-options. For example, when I type `ls -l -a /home/fuz /var/log`, then `-l` and `-a` are options, `/home/fuz` and `/var/log` are operands. GNU getopt_long has the quirk that it likes to reorder the argument list. This is going to make implementing what you want somewhat tricky, which is why I'm trying to find different ways to get the desired functionality. – fuz Jan 13 '16 at 11:20
  • I do not think I can accept options and operands in any other form than that. If possible please elaborate a little more about the info that it reorder the argument list. Also if possible tell me about a possible solution or point to somewhere where I can read about this. I read the man page for 3 times at this point and still no luck with options with multiple operands. – Saik Jan 13 '16 at 11:30
  • Also if it is much simpler to assume that command option will have constant number of arguments, I will be quite happy with that as well at this poing. – Saik Jan 13 '16 at 11:35
  • you can pass all the arguments in a string separated by some character (-, +, space....). Something like : --command "arg1-arg2-arg3",......... and then parse the string in your program. – Abhishek Choubey Jan 13 '16 at 11:39
  • Abhishek the problem here is that the input to a program is well defined and I cannot call the program in the way that you described... – Saik Jan 13 '16 at 11:42
  • I believe this is a duplicate question. You can find answers [here](http://stackoverflow.com/a/37414143/5780009) – deviouswhisperingchicken May 24 '16 at 12:54

0 Answers0