Commit 3a967c59 authored by Doug Huff's avatar Doug Huff Committed by Facebook GitHub Bot

NestedCommandLineApp Add -h as an alias for --help globally and to all subcommands

Summary: Alias `-h` to `--help` because we live in a society.

Reviewed By: yfeldblum

Differential Revision: D27370742

fbshipit-source-id: eb346546fe4066eef62313176ff7e47c20e507be
parent 552024d3
...@@ -72,6 +72,7 @@ NestedCommandLineApp::NestedCommandLineApp( ...@@ -72,6 +72,7 @@ NestedCommandLineApp::NestedCommandLineApp(
displayHelp(vm, args); displayHelp(vm, args);
}); });
builtinCommands_.insert(kHelpCommand); builtinCommands_.insert(kHelpCommand);
addAlias(kShortHelpCommand.str(), kHelpCommand.str());
addCommand( addCommand(
kVersionCommand.str(), kVersionCommand.str(),
...@@ -84,7 +85,7 @@ NestedCommandLineApp::NestedCommandLineApp( ...@@ -84,7 +85,7 @@ NestedCommandLineApp::NestedCommandLineApp(
builtinCommands_.insert(kVersionCommand); builtinCommands_.insert(kVersionCommand);
globalOptions_.add_options()( globalOptions_.add_options()(
kHelpCommand.str().c_str(), (kHelpCommand.str() + "," + kShortHelpCommand.str()).c_str(),
"Display help (globally or for a given command)")( "Display help (globally or for a given command)")(
kVersionCommand.str().c_str(), "Display version information"); kVersionCommand.str().c_str(), "Display version information");
} }
......
...@@ -61,6 +61,7 @@ class NestedCommandLineApp { ...@@ -61,6 +61,7 @@ class NestedCommandLineApp {
Command; Command;
static constexpr StringPiece const kHelpCommand = "help"; static constexpr StringPiece const kHelpCommand = "help";
static constexpr StringPiece const kShortHelpCommand = "h";
static constexpr StringPiece const kVersionCommand = "version"; static constexpr StringPiece const kVersionCommand = "version";
/** /**
* Initialize the app. * Initialize the app.
......
...@@ -75,6 +75,10 @@ TEST(ProgramOptionsTest, Errors) { ...@@ -75,6 +75,10 @@ TEST(ProgramOptionsTest, Errors) {
TEST(ProgramOptionsTest, Help) { TEST(ProgramOptionsTest, Help) {
// Not actually checking help output, just verifying that help doesn't fail // Not actually checking help output, just verifying that help doesn't fail
callHelper({"--version"}); callHelper({"--version"});
callHelper({"--h"});
callHelper({"--h", "foo"});
callHelper({"--h", "bar"});
callHelper({"--h", "--", "bar"});
callHelper({"--help"}); callHelper({"--help"});
callHelper({"--help", "foo"}); callHelper({"--help", "foo"});
callHelper({"--help", "bar"}); callHelper({"--help", "bar"});
...@@ -84,6 +88,7 @@ TEST(ProgramOptionsTest, Help) { ...@@ -84,6 +88,7 @@ TEST(ProgramOptionsTest, Help) {
callHelper({"help", "bar"}); callHelper({"help", "bar"});
// wrong command name // wrong command name
callHelper({"-h", "qux"}, 1);
callHelper({"--help", "qux"}, 1); callHelper({"--help", "qux"}, 1);
callHelper({"help", "qux"}, 1); callHelper({"help", "qux"}, 1);
...@@ -93,6 +98,7 @@ TEST(ProgramOptionsTest, Help) { ...@@ -93,6 +98,7 @@ TEST(ProgramOptionsTest, Help) {
TEST(ProgramOptionsTest, DevFull) { TEST(ProgramOptionsTest, DevFull) {
folly::File full("/dev/full", O_RDWR); folly::File full("/dev/full", O_RDWR);
callHelper({"-h"}, 1, full.fd());
callHelper({"--help"}, 1, full.fd()); callHelper({"--help"}, 1, full.fd());
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment