Article View: comp.unix.programmer
Article #3751Re: methods for parsing commandline switches or arguments
From: Tarek
Date: Sun, 14 Apr 2002 09:32
Date: Sun, 14 Apr 2002 09:32
76 lines
3949 bytes
3949 bytes
--> 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!!!
Technical Details
Message-ID:
<b6c86c4b.0204140132.3eb6ae6d@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mailReferences:
<9e8ebeb2.0204131900.67ea8c47@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: 1014db,66fe05f085d006dd X-Google-Attributes: gid1014db,public X-Google-Thread: fc89c,66fe05f085d006dd X-Google-Attributes: gidfc89c,public X-Google-Thread: 109fba,66fe05f085d006dd X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2002-04-14 02:32:55 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: tabuelnaga@hotmail.com (Tarek) Newsgroups: comp.unix.programmer,comp.lang.c,comp.lang.c++,gnu.utils.help Subject: Re: methods for parsing commandline switches or arguments Date: 14 Apr 2002 02:32:54 -0700 Organization: http://groups.google.com/ Lines: 76 Message-ID: <b6c86c4b.0204140132.3eb6ae6d@posting.google.com> References: <9e8ebeb2.0204131900.67ea8c47@posting.google.com> NNTP-Posting-Host: 194.79.121.35 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1018776775 27185 127.0.0.1 (14 Apr 2002 09:32:55 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 14 Apr 2002 09:32:55 GMT