getopt example please

simendsjo simendsjo at gmail.com
Wed Feb 26 02:12:03 PST 2014


On 02/26/2014 11:06 AM, Tobias Pankrath wrote:
> On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards wrote:
>> Request a small example of how to use getopt to accomplish the following:
>>
>> [1] program subcommand //process subcommand with default arguments if any
>> [2] program -h subcommand //output help information about subcommand
>> [3] program subcommand --option1 --option2 true option3=log.txt //
>> process subcommand with user specified options
>>
>> Thanks,
>> Andrew
>
> This is not fully automated with getopt. You could use getopt to parse
> all non-positional arguments and than parse the rest by hand.
> --
> getopt(args, ...);
> --
> Now, args[1] (or [0]?) should be "subcommand".

args[0] is the program itself. As Tobias mention, you can check the 
first subcommand and use a different getopt strategy for each.

It's possible to say to getopt that it should not throw an error when it 
encounters unknown parameters so you can parse them yourself.


More information about the Digitalmars-d-learn mailing list