LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2016, 01:03 AM   #1
Alpha90
Member
 
Registered: Jul 2012
Posts: 97

Rep: Reputation: Disabled
Is there a standard way to differentiate command line arguments and flags in C


I am learning how to work with command line applications in C and I am trying to see if there is a function in the C or Posix standard libraries that deals with differentiating flags from arguments basically the inverse of getopt().

Basically I am trying to make a program that takes an arbitrary input.

Eg.

Code:
Foo -a 10 bar1 bar2 bar3 -a 20 bar4
Where the flag -a accepts a numerical value and bar* are arguments for the program to deal with.
 
Old 03-01-2016, 02:15 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,905

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
http://stackoverflow.com/questions/1...line-arguments (the last example) probably helps you
 
Old 03-01-2016, 02:44 AM   #3
Alpha90
Member
 
Registered: Jul 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
I am confuses on what you mean? I am writing a C program not a bash script.
 
Old 03-01-2016, 02:45 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,905

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
about the algorithm to use.
 
Old 03-01-2016, 08:50 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
There are getopt() and getopt_long() functions in the C library. They work in a manner much like the corresponding shell commands. See `man 3 getopt` for details. I guess I don't understand what you mean by "the inverse of getopt()."

Last edited by rknichols; 03-01-2016 at 08:53 AM.
 
Old 03-01-2016, 12:08 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Getopt() relies on the - or -- flags to identify and segregate options. I have a program which uses it and staged some changes to see if it would grab the added stuff not identified with a dash option before it and it didn't work out.

Probably what you're looking for doesn't exist in native form so you'd have to grab the source for getopt() and modify it to suit.
 
Old 03-01-2016, 12:25 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
I agree with rtmistler, I guess I don't understand what you mean by "the inverse of getopt()."

Rather than re-inventing a pretty good wheel, maybe you should adapt your arg format and handlers to work within getopt or getopt_long (my favorite).

What you seem to be asking is for a way to pass multiple values to a single option. The obvious easy way to do that would be to aggregate those values into a single string value that could be recovered by getopt(_long), then have your code break out and handle those sub-values.

For example, instead of multiple, space separated values for option -a...

Code:
Foo -a 10 bar1 bar2 bar3 -a 20 bar4
... require a quoted or contiguous comma separated list for each option value...


Code:
Foo -a 10,bar1,bar2,bar3 -a 20,bar4
... then in your case statement for -a iterate over the list found in optarg... done.

This would allow for variable length of the value lists per option as well.

Last edited by astrogeek; 03-01-2016 at 12:47 PM.
 
Old 03-01-2016, 02:11 PM   #8
Alpha90
Member
 
Registered: Jul 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
I definitely did not do a good job explaining what I meant last night. I guess it was too long a night.

To give an example of what I am talking about or atleast attempting to talk about I will use 'man 1 head' as an example.

Code:
head -n 20 FILE1 FILE2 FILE3 -n 10 FILE4 FILE5 ...
While that is basically is an extremely arbitrary example it still executes at least on GNU head. I know I can use a simple loop to set the flag variable with getopt with -n eventually being set to 10. What I am trying to figure out is there a function to instead parse out the flags from argv and flag arguments leaving me in my example just what is in green. Do I need to make a loop to run through argv and ignoring everything that starts with a '-' or is there a standard/portable function I can use that I can use to parse the non option flags in the array.
 
Old 03-01-2016, 02:20 PM   #9
OregonJim
Member
 
Registered: Feb 2016
Posts: 98

Rep: Reputation: Disabled
If I understand you correctly, I think you're looking for the functions in the popt library:

http://directory.fsf.org/wiki/Popt
 
Old 03-01-2016, 02:49 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by Alpha90 View Post
To give an example of what I am talking about or atleast attempting to talk about I will use 'man 1 head' as an example.

Code:
head -n 20 FILE1 FILE2 FILE3 -n 10 FILE4 FILE5 ...
While that is basically is an extremely arbitrary example it still executes at least on GNU head. I know I can use a simple loop to set the flag variable with getopt with -n eventually being set to 10. What I am trying to figure out is there a function to instead parse out the flags from argv and flag arguments leaving me in my example just what is in green. Do I need to make a loop to run through argv and ignoring everything that starts with a '-' or is there a standard/portable function I can use that I can use to parse the non option flags in the array.
Well, by default getopt works exactly as in your example.

The -n 20 is NOT associated with the file names following it and before -n 10. What happens is that -n is set to 20, then reset to 10 and all filenames are permuted to the end of the arg list then processed after both -n options.

From here:

Code:
getopt has three ways to deal with options that follow non-options argv elements. 
The special argument ‘--’ forces in all cases the end of option scanning.

    * The default is to permute the contents of argv while scanning it so that eventually all the non-options
    are at the end. This allows options to be given in any order, even with programs that were not written to expect this.
    * If the options argument string begins with a hyphen (‘-’), this is treated specially. It permits 
    arguments that are not options to be returned as if they were associated with option character ‘\1’.
    * POSIX demands the following behavior: The first non-option stops option processing. This mode is 
    selected by either setting the environment variable POSIXLY_CORRECT or beginning the options argument 
    string with a plus sign (‘+’).
The first is how your head example works - is that actually what you want?
 
Old 03-02-2016, 12:50 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,905

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by Alpha90 View Post
Do I need to make a loop to run through argv and ignoring everything that starts with a '-' or is there a standard/portable function I can use that I can use to parse the non option flags in the array.
That's what I posted, and I think the only way is to check arguments one by one and decide what to do. What you wanted to achieve is "unusual" therefore there is no common solution.
 
Old 03-10-2016, 09:27 PM   #12
Alpha90
Member
 
Registered: Jul 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
Sorry for the slow reply. astrogeek is correct that is what I was looking for. I overlooked that in the man page. Thank you all for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hyperactive alias overrides all command line flags. philwynk Linux - Newbie 3 02-04-2008 07:04 PM
need some help regarding command line arguments kristam269 Linux - General 1 01-23-2007 09:40 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
Command line arguments?? almagerenia Linux - Newbie 1 09-08-2006 04:05 AM
command line arguments containing ( Lotharster Linux - Newbie 3 01-05-2006 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration