Thread overview
getopt example please
Feb 26, 2014
Andrew Edwards
Feb 26, 2014
Tobias Pankrath
Feb 26, 2014
simendsjo
Feb 26, 2014
Vladimir Panteleev
Feb 26, 2014
Andrew Edwards
February 26, 2014
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
February 26, 2014
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".
February 26, 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.
February 26, 2014
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

import std.getopt;

void main(string[] args)
{
    bool help;
    getopt(args,
        "h|help", &help,
        config.stopOnFirstNonOption,
    );

    if (args.length == 1)
    {
        // Print general help and list of commands
        return;
    }

    auto subcommand = args[1];
    switch (subcommand)
    {
        case "subcommand":
        {
            if (help)
            {
                // Print help for subcommand
                return;
            }

            bool option1;
            string option2, option3;
            getopt(args,
                "option1", &option1,
                "option2", &option2,
                "option3", &option3,
            );

            // Process subcommand with specified options

            break;
        }
        default:
            throw new Exception("Unknown subcommand "~ subcommand);
    }
}

February 26, 2014
On 2/26/14, 5:37 AM, Vladimir Panteleev 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
>
> import std.getopt;
>
> void main(string[] args)
> {
>      bool help;
>      getopt(args,
>          "h|help", &help,
>          config.stopOnFirstNonOption,
>      );
>
>      if (args.length == 1)
>      {
>          // Print general help and list of commands
>          return;
>      }
>
>      auto subcommand = args[1];
>      switch (subcommand)
>      {
>          case "subcommand":
>          {
>              if (help)
>              {
>                  // Print help for subcommand
>                  return;
>              }
>
>              bool option1;
>              string option2, option3;
>              getopt(args,
>                  "option1", &option1,
>                  "option2", &option2,
>                  "option3", &option3,
>              );
>
>              // Process subcommand with specified options
>
>              break;
>          }
>          default:
>              throw new Exception("Unknown subcommand "~ subcommand);
>      }
> }
>

Thank you very much.