🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3750

methods for parsing commandline switches or arguments

#3750
From: 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!!!
Technical Details
Message-ID: <9e8ebeb2.0204131900.67ea8c47@posting.google.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109f73,66fe05f085d006dd,start
X-Google-Attributes: gid109f73,public
X-Google-Thread: fc89c,66fe05f085d006dd,start
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 1014db,66fe05f085d006dd,start
X-Google-Attributes: gid1014db,public
X-Google-Thread: 109fba,66fe05f085d006dd,start
X-Google-Attributes: gid109fba,public
X-Google-ArrivalTime: 2002-04-13 20:00:15 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail
From: gnuist007@hotmail.com (gnuist)
Newsgroups: comp.unix.programmer,comp.lang.c,comp.lang.c++,gnu.utils.help
Subject: methods for parsing commandline switches or arguments
Date: 13 Apr 2002 20:00:14 -0700
Organization: http://groups.google.com/
Lines: 31
Message-ID: <9e8ebeb2.0204131900.67ea8c47@posting.google.com>
NNTP-Posting-Host: 209.179.38.41
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1018753215 18068 127.0.0.1 (14 Apr 2002 03:00:15 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 14 Apr 2002 03:00:15 GMT