standar command line argument parser

  • Follow


Hello all,

I was wondering if there is a C/C++ command line parser that works for
linux AND windows. I tried posting this in the comp.lang.c group but
half of the replys were flames about it not being in the right group.

Anyways, the parser im looking for should be able to support multiple
arguments per option.

Example:   $>  myProg --option1 arg arg arg --option2 arg --option3 arg
arg arg.

If there is one that does this and runs in both windows and linux that
would be perfect.

I have looked at GetOpt, it is such a pain in the ass in the Windows
environmen (im using visual studio for windows development).

If you know of one, please let me know.

Thanks

0
Reply 31337one (15) 7/28/2006 10:58:50 PM

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

31337one@gmail.com writes:

> I was wondering if there is a C/C++ command line parser that works for
> linux AND windows.

There's nothing platform-dependent about a command-line parser.  All
it's doing is processing argv[].  getopt will work equally well on
both platforms.

If you are using C++, you might find Boost.Program_options is more
suitable.

  http://www.boost.org/doc/html/program_options.html

You can find an example of its use here:
  http://people.debian.org/~rleigh/schroot-1.0.0.tar.bz2

Boost.Program_options, unlike gettext or GNU gettext, supports full
internationalisation.  It also integrates with the C++ Standary
Library, and supports parsing of arbitrary datatypes using templates.

$ schroot --help
Usage:
  schroot  [OPTION=E2=80=A6] [COMMAND] =E2=80=94 run command or shell in a =
chroot

General options:
  -h [ --help ]         Show help options
  -V [ --version ]      Print version information
  -q [ --quiet ]        Show less output
  -v [ --verbose ]      Show more output
  -l [ --list ]         List available chroots
  -i [ --info ]         Show information about selected chroots
  --config              Dump configuration of selected chroots
  --location            Print location of selected chroots

Chroot selection:
  -c [ --chroot ] arg   Use specified chroot
  -a [ --all ]          Select all chroots and active sessions
  --all-chroots         Select all chroots
  --all-sessions        Select all active sessions

Chroot environment:
  -u [ --user ] arg              Username (default current user)
  -p [ --preserve-environment ]  Preserve user environment

Session management:
  -b [ --begin-session ]  Begin a session; returns a session ID
  --recover-session       Recover an existing session
  -r [ --run-session ]    Run an existing session
  -e [ --end-session ]    End an existing session
  -f [ --force ]          Force operation, even if it fails


$ LANGUAGE=3Dde schroot --help
Verwendung:
  schroot  [OPTION...] [KOMMANDO] - startet ein Kommando oder eine Shell in=
 einem chroot

Allgemeine Optionen:
  -h [ --help ]         Zeigt Hilfeoptionen
  -V [ --version ]      Versionsinformationen anzeigen
  -q [ --quiet ]        Weniger Ausgaben zeigen
  -v [ --verbose ]      Mehr Ausgaben zeigen
  -l [ --list ]         Verf=C3=BCgbare chroots anzeigen
  -i [ --info ]         Informationen =C3=BCber gew=C3=A4hlte chroots anzei=
gen
  --config              Konfiguration der gew=C3=A4hlten chroots ausgeben
  --location            Ort der gew=C3=A4hlten chroots ausgeben

Chroot-Auswahl:
  -c [ --chroot ] arg   Angegebenen chroot verwenden
  -a [ --all ]          Alle chroots und aktiven Sitzungen ausgeben
  --all-chroots         Alle chroots ausw=C3=A4hlen
  --all-sessions        Alle aktiven Sitzungen ausw=C3=A4hlen

Chroot-Umgebung:
  -u [ --user ] arg              Benutzername (standardm=C3=A4=C3=9Fig der =
aktuelle=20
                                 Benutzer)
  -p [ --preserve-environment ]  Benutzerumgebungsvariablen beibehalten

Sitzungsverwaltung:
  -b [ --begin-session ]  Start einer Sitzung; gibt die Sitzungs-ID zur=C3=
=BCck
  --recover-session       Wiederherstellen einer existierenden Sitzung
  -r [ --run-session ]    Eine existierende Sitzung starten
  -e [ --end-session ]    Eine existierende Sitzung beenden
  -f [ --force ]          Operation erzwingen, selbst wenn sie fehlschl=C3=
=A4gt


=2D-=20
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please sign and encrypt your mail.

--=-=-=
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFEyp+CVcFcaSW/uEgRAgBlAJ9vByWWr2yghW8BD3SZbhmWp1/YtACcCmy+
msUo7jH66KSSM/plDMP4ozc=
=jKBZ
-----END PGP SIGNATURE-----
--=-=-=--
0
Reply Roger 7/28/2006 11:36:29 PM


On Fri, 28 Jul 2006 15:58:50 -0700, 31337one wrote:

> Hello all,
> 
> I was wondering if there is a C/C++ command line parser that works for
> linux AND windows. I tried posting this in the comp.lang.c group but half
> of the replys were flames about it not being in the right group.

Anything written in standard C will work in both, as both GCC and Visual
Studio are capable of compiling standard C code. There might be a chance a
line or two in a particular option parser implementation has something
Unix-specific (or otherwise non-standard), but that should be easily
remedied.

> Anyways, the parser im looking for should be able to support multiple
> arguments per option.
> 
> Example:   $>  myProg --option1 arg arg arg --option2 arg --option3 arg
> arg arg.

If this is what you need, then almost certainly you will not find any
existing source file or library to do this. What if the second
value itself began w/ the option prefix (e.g. --option1 foo --bar)?
However, this should be supported by any getopt_long() variant:

	myProg --option1 arg --option1 arg --option1 arg

And in your program you just insert them into an array or list yourself as
the parser feeds them to you (it shouldn't discard multiple options w/ the
same name).

> If there is one that does this and runs in both windows and linux that
> would be perfect.

Again, GNU getopt() or *BSD getopt_long() is freely available. Have you
tried them since they were suggested in comp.lang.c?

> I have looked at GetOpt, it is such a pain in the ass in the Windows
> environmen (im using visual studio for windows development).

How so? If a "pain in the ass" means that it's something short of working
w/o any effort whatsoever, then you're certainly SOL.

By "GetOpt" do you mean the reference version I've heard is available in
Visual Studio, or the ones suggested above?

0
Reply William 7/28/2006 11:56:24 PM

On Fri, 28 Jul 2006 15:58:50 -0700, 31337one wrote:
> Hello all,
> 
> I was wondering if there is a C/C++ command line parser that works for
> linux AND windows. I tried posting this in the comp.lang.c group but half
> of the replys were flames about it not being in the right group.
> 

For the record, comp.lang.c is, IMO, the best place on the entire
Internet, and maybe the entire globe, to learn and discuss the C language.
This is primarily so because the folks there relentlessly and militantly
keep the discussion on topic.

0
Reply William 7/29/2006 12:00:52 AM

William Ahern wrote:
> On Fri, 28 Jul 2006 15:58:50 -0700, 31337one wrote:
> > Hello all,
> >
> > I was wondering if there is a C/C++ command line parser that works for
> > linux AND windows. I tried posting this in the comp.lang.c group but half
> > of the replys were flames about it not being in the right group.
> >
>
> For the record, comp.lang.c is, IMO, the best place on the entire
> Internet, and maybe the entire globe, to learn and discuss the C language.
> This is primarily so because the folks there relentlessly and militantly
> keep the discussion on topic.

LOL, i tried posting there. They told me it was the wrong group for
this kind of thing.

0
Reply 31337one 8/2/2006 4:21:45 PM

William Ahern wrote:
> On Fri, 28 Jul 2006 15:58:50 -0700, 31337one wrote:
>
> > Hello all,
> >
> > I was wondering if there is a C/C++ command line parser that works for
> > linux AND windows. I tried posting this in the comp.lang.c group but half
> > of the replys were flames about it not being in the right group.
>
> Anything written in standard C will work in both, as both GCC and Visual
> Studio are capable of compiling standard C code. There might be a chance a
> line or two in a particular option parser implementation has something
> Unix-specific (or otherwise non-standard), but that should be easily
> remedied.
>
> > Anyways, the parser im looking for should be able to support multiple
> > arguments per option.
> >
> > Example:   $>  myProg --option1 arg arg arg --option2 arg --option3 arg
> > arg arg.
>
> If this is what you need, then almost certainly you will not find any
> existing source file or library to do this. What if the second
> value itself began w/ the option prefix (e.g. --option1 foo --bar)?
> However, this should be supported by any getopt_long() variant:
>
> 	myProg --option1 arg --option1 arg --option1 arg
>
> And in your program you just insert them into an array or list yourself as
> the parser feeds them to you (it shouldn't discard multiple options w/ the
> same name).
>
> > If there is one that does this and runs in both windows and linux that
> > would be perfect.
>
> Again, GNU getopt() or *BSD getopt_long() is freely available. Have you
> tried them since they were suggested in comp.lang.c?
>
> > I have looked at GetOpt, it is such a pain in the ass in the Windows
> > environmen (im using visual studio for windows development).
>
> How so? If a "pain in the ass" means that it's something short of working
> w/o any effort whatsoever, then you're certainly SOL.
>
> By "GetOpt" do you mean the reference version I've heard is available in
> Visual Studio, or the ones suggested above?


I have searched the internet for getopt_long and I cannot find the .c
and .h files. Why are they so hard to locate to download?

0
Reply 31337one 8/2/2006 4:25:26 PM

31337one@gmail.com wrote:
> William Ahern wrote:
>> On Fri, 28 Jul 2006 15:58:50 -0700, 31337one wrote:
>>> Hello all,
>>>
>>> I was wondering if there is a C/C++ command line parser that works for
>>> linux AND windows. I tried posting this in the comp.lang.c group but half
>>> of the replys were flames about it not being in the right group.
>>>
>> For the record, comp.lang.c is, IMO, the best place on the entire
>> Internet, and maybe the entire globe, to learn and discuss the C language.
>> This is primarily so because the folks there relentlessly and militantly
>> keep the discussion on topic.
> 
> LOL, i tried posting there. They told me it was the wrong group for
> this kind of thing.

Yes. His point is that it's the right place to discuss the C language, 
not the place to discuss command line parsers which might happen to be 
written in C.
0
Reply Henry 8/2/2006 4:54:57 PM

6 Replies
256 Views

(page loaded in 0.11 seconds)

Similiar Articles:













7/25/2012 5:05:59 PM


Reply: