[ 
https://issues.apache.org/jira/browse/MESOS-2711?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Marco Massenzio updated MESOS-2711:
-----------------------------------
    Description: 
When using {{stout/flags/flags.hpp}} and implementing our own {{Flags}} class 
(derived from {{flags::FlagsBase}}) pretty much everyone ends up 
re-implementing some variant of the following:

{code}
void usage(const char* argv0, const flags::FlagsBase& flags)
{
  cerr << "Usage: " << os::basename(argv0).get() << " [...]" << endl
       << endl
       << "Supported options:" << endl
       << flags.usage();
}

{code}

and in {{main.cpp}}:
{code}
int main(int argc, char** argv)
{
  bool help;
  flags.add(&help,
            "help",
            "Prints this help message",
            false);
  // ...
  if (load.isError()) {
    cerr << load.error() << endl;
    usage(argv[0], flags);
    exit(1);
  }

  if (help) {
    usage(argv[0], flags);
    exit(1);
  }
}
{code}

Note that {{FlagsBase}} already "knows" {{argv[0]}} and it would be nice if it 
implemented the "help" functionality itself, so that everyone immediately gains 
access to it; further, the {{printUsage()}} method could be implemented easily 
by simply adding the facility for derived classes to specify some form of a 
"blurb" (the bit that is emitted between the {{Usage:}} line and the 
{{Options:}} body and, possibly, to customize the "usage" one-liner.

  was:
When using {{stout/flags/flags.hpp}} and implementing our own {{Flags}} class 
(derived from {{flags::FlagsBase}}) pretty much everyone ends up 
re-implementing some variant of the following:

{code}
void printUsage(const string& prog, const string& options)
{
  cout << "Usage: " << os::basename(prog).get() << " [options]\n\n"
      "One (and only one) of the following options MUST be present.\n\n"
      "Options:\n" << options << endl;
}
{code}

and in {{main.cpp}}:
{code}
int main(int argc, char** argv)
{
  MyFlags flags;
  bool help;
  flags.add(&help, "help", "Prints this help message", false);

  Try<Nothing> load = flags.load(None(), argc, argv);
  // ...
  if (help) {
      printUsage(argv[0], flags.usage());
  }
  // ...
}
{code}

Note that {{FlagsBase}} already "knows" {{argv[0]}} and it would be nice if it 
implemented the "help" functionality itself, so that everyone immediately gains 
access to it; further, the {{printUsage()}} method could be implemented easily 
by simply adding the facility for derived classes to specify some form of a 
"blurb" (the bit that is emitted between the {{Usage:}} line and the 
{{Options:}} body and, possibly, to customize the "usage" one-liner.


> Add some commonly-used utility to stout/flags
> ---------------------------------------------
>
>                 Key: MESOS-2711
>                 URL: https://issues.apache.org/jira/browse/MESOS-2711
>             Project: Mesos
>          Issue Type: Improvement
>          Components: stout
>    Affects Versions: 0.22.0
>            Reporter: Marco Massenzio
>            Assignee: Marco Massenzio
>            Priority: Minor
>
> When using {{stout/flags/flags.hpp}} and implementing our own {{Flags}} class 
> (derived from {{flags::FlagsBase}}) pretty much everyone ends up 
> re-implementing some variant of the following:
> {code}
> void usage(const char* argv0, const flags::FlagsBase& flags)
> {
>   cerr << "Usage: " << os::basename(argv0).get() << " [...]" << endl
>        << endl
>        << "Supported options:" << endl
>        << flags.usage();
> }
> {code}
> and in {{main.cpp}}:
> {code}
> int main(int argc, char** argv)
> {
>   bool help;
>   flags.add(&help,
>             "help",
>             "Prints this help message",
>             false);
>   // ...
>   if (load.isError()) {
>     cerr << load.error() << endl;
>     usage(argv[0], flags);
>     exit(1);
>   }
>   if (help) {
>     usage(argv[0], flags);
>     exit(1);
>   }
> }
> {code}
> Note that {{FlagsBase}} already "knows" {{argv[0]}} and it would be nice if 
> it implemented the "help" functionality itself, so that everyone immediately 
> gains access to it; further, the {{printUsage()}} method could be implemented 
> easily by simply adding the facility for derived classes to specify some form 
> of a "blurb" (the bit that is emitted between the {{Usage:}} line and the 
> {{Options:}} body and, possibly, to customize the "usage" one-liner.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to