Article View: comp.unix.programmer
Article #3752Re: methods for parsing commandline switches or arguments
From: Michael Borella
Date: Sun, 14 Apr 2002 12:32
Date: Sun, 14 Apr 2002 12:32
88 lines
4533 bytes
4533 bytes
There are a couple of tools that will do the parsing for you and
fill out a data structure with the results. Genparse is one
http://genparse.sf.net
On 14 Apr 2002 02:32:54 -0700, Tarek <tabuelnaga@hotmail.com> wrote:
> --> I'm not sure what you really need .. But anyway,
> C uses argc and argv to parse the command line ..
> This means that you need to include them in your code
> main (int argc, char *argv[])
> If your program's name is "utility" and you type just utility, this
> means that you have argc=1 and argv[0]= utility
> --------------------------------------------------------
> One simple way to do something like that ..
> #include <stdio.h>
> int main(int argc, char *argv[])
> {
> int i ;
> struct param
> {
> char option[20] ;
> char value[20] ;
> } ;
>
> struct param my_param ;
>
> if (argc > 1)
> {
> for (i = 1 ; i < argc ; i=i+2)
> {
> strcpy(my_param.option,argv[i]) ;
> strcpy(my_param.value,argv[i+1]) ;
> /* write whatever you want here to make use of the parameters
> user send on the command line
> If the user writes > utility -option1 value
> then you have my_param.option = -option1
> and my_param.value = value
>
> you can also make an array of this structure if you expect
> many arguments to be sent !
> */
> }
> }
> }
>
> Hope, this help you .. ! :)
>
> T.N.
> ----------------------------------------------------
>
> gnuist007@hotmail.com (gnuist) wrote in message news:<9e8ebeb2.0204131900.67ea8c47@posting.google.com>...
>> Suppose that I am writing a utility
>>
>> utility [-mode1 <24|124>] [-mode2 [67]]
>>
>> so if I am correct in using the BNF notation, [] denotes optional arguments
>> and <> denotes mandatory arguments. The command egrep in linux has alternate
>> patterns for searching specified by the vertical stroke, "|" as we have used
>> for mode number one for this utility. I have used numbers as the arguments
>> to the switches to show this possibility.
>>
>> I have to parse this command line via *argv[] and argc.
>> Please show how to approach it. For example, what argc will different
>> arguments give?
>>
>> utility -mode1 "24|214"
>> utility -mode1 24|214
>> utility -mode2 24
>> utility -mode2 "24" -mode2 67
>>
>> and how will we deal with conflicting arguments? Is there are uniform and
>> professional method of approaching this problem? I am not a CS and hardly
>> know the finite state machines but if you are kind enough to put some examples
>> to plant the ideas, would be very grateful and finish this work. I do have
>> the dragon book.
>>
>> I am posting this to C/C++ because they are the languages of implementation
>> and unix because they also deal with it and GNU compatible utilities require
>> robust and unambiguous command line interface as a basic requirement to
>> consider any to be considered gnuable.
>>
>> Thanks a lot for any help and hope to give you some good utilities in return!!!
--
Mike Borella
cathmike@xnet.com
http://www.borella.net
Technical Details
Message-ID:
<slrnabitnp.ro8.cathmike@typhoon.xnet.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.xnet.com!cathmikeReferences:
<9e8ebeb2.0204131900.67ea8c47@posting.google.com> <b6c86c4b.0204140132.3eb6ae6d@posting.google.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109f73,66fe05f085d006dd X-Google-Attributes: gid109f73,public X-Google-Thread: fc89c,66fe05f085d006dd X-Google-Attributes: gidfc89c,public X-Google-Thread: 109fba,66fe05f085d006dd X-Google-Attributes: gid109fba,public X-Google-Thread: 1014db,66fe05f085d006dd X-Google-Attributes: gid1014db,public X-Google-ArrivalTime: 2002-04-14 05:32:59 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.xnet.com!cathmike From: cathmike@news.xnet.com (Michael Borella) Newsgroups: comp.unix.programmer,comp.lang.c,comp.lang.c++,gnu.utils.help Subject: Re: methods for parsing commandline switches or arguments Followup-To: gnu.utils.help Date: Sun, 14 Apr 2002 12:32:57 +0000 (UTC) Organization: XNet Information Systems (Winstar) Lines: 88 Message-ID: <slrnabitnp.ro8.cathmike@typhoon.xnet.com> References: <9e8ebeb2.0204131900.67ea8c47@posting.google.com> <b6c86c4b.0204140132.3eb6ae6d@posting.google.com> Reply-To: cathmike@xnet.com NNTP-Posting-Host: typhoon.xnet.com X-Trace: flood.xnet.com 1018787577 25918 198.147.221.66 (14 Apr 2002 12:32:57 GMT) X-Complaints-To: abuse@xnet.com NNTP-Posting-Date: Sun, 14 Apr 2002 12:32:57 +0000 (UTC) User-Agent: slrn/0.9.7.0 (SunOS)