🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.programmer
4 messages
4 total messages Started by gnuist Sun, 14 Apr 2002 03:00
methods for parsing commandline switches or arguments
#3750
Author: gnuist
Date: Sun, 14 Apr 2002 03:00
31 lines
2470 bytes
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!!!
Re: methods for parsing commandline switches or arguments
#3751
Author: Tarek
Date: Sun, 14 Apr 2002 09:32
76 lines
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!!!
Re: methods for parsing commandline switches or arguments
#3752
Author: Michael Borella
Date: Sun, 14 Apr 2002 12:32
88 lines
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 
Re: methods for parsing commandline switches or arguments
#3753
Author: David Thompson
Date: Mon, 15 Apr 2002 04:01
13 lines
1622 bytes
"gnuist" <gnuist007@hotmail.com> wrote
[snip]
> I have to parse this command line via *argv[] and argc.
> Please show how to approach it.
[snip]

Check out this library,

  http://256.com/sources/argv/argv.html

--
David Thompson
dat1965@yahoo.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads