From 037550b7915f70557c893dd89b4cb66fbb668c18 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Sat, 18 May 2019 18:40:26 +0200 Subject: [PATCH] '-h' should show help Signed-off-by: Thomas Lauf --- src/CLI.cpp | 9 ++++----- src/init.cpp | 6 ++++-- test/help.t | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/CLI.cpp b/src/CLI.cpp index 65141983..fd164e0a 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2006 - 2016, Paul Beckingham, Federico Hernandez. +// Copyright 2006 - 2019, Thomas Lauf, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -445,10 +445,9 @@ void CLI::canonicalizeNames () alreadyFoundCmd = true; } - // 'timew --help' should be treated the same as 'timew help - // '. Therefore, "--help" on the command line should always - // become the command. - else if (alreadyFoundCmd && (raw == "--help")) + // 'timew --help|-h' should be treated the same as 'timew help '. + // Therefore, '--help|-h' on the command line should always become the command. + else if (alreadyFoundCmd && (raw == "--help" || raw == "-h")) { for (auto& b : _args) { if (b.hasTag("CMD")) diff --git a/src/init.cpp b/src/init.cpp index 18418644..5a371e2d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1,6 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // -// Copyright 2016 - 2018, Thomas Lauf, Paul Beckingham, Federico Hernandez. +// Copyright 2016 - 2019, Thomas Lauf, Paul Beckingham, Federico Hernandez. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -62,6 +62,7 @@ void initializeEntities (CLI& cli) cli.entity ("command", "get"); cli.entity ("command", "help"); cli.entity ("command", "--help"); + cli.entity ("command", "-h"); cli.entity ("command", "join"); cli.entity ("command", "lengthen"); cli.entity ("command", "modify"); @@ -273,7 +274,8 @@ int dispatchCommand ( else if (command == "gaps") status = CmdGaps (cli, rules, database ); else if (command == "get") status = CmdGet (cli, rules, database ); else if (command == "help" || - command == "--help") status = CmdHelp (cli, extensions); + command == "--help" || + command == "-h") status = CmdHelp (cli, extensions); else if (command == "join") status = CmdJoin (cli, rules, database ); else if (command == "lengthen") status = CmdLengthen (cli, rules, database ); else if (command == "modify") status = CmdModify (cli, rules, database ); diff --git a/test/help.t b/test/help.t index 53d146a1..24794484 100755 --- a/test/help.t +++ b/test/help.t @@ -46,6 +46,18 @@ class TestHelp(TestCase): code, out, err = self.t("help") self.assertRegexpMatches(out, r"Usage: timew [--version]") + def test_help_long_option_should_print_usage(self): + """timew --help should print usage""" + code, out1, err1 = self.t("help") + code, out2, err2 = self.t("--help") + self.assertEquals(out1, out2) + + def test_help_short_option_should_print_usage(self): + """timew -h should print usage""" + code, out1, err1 = self.t("help") + code, out2, err2 = self.t("-h") + self.assertEquals(out1, out2) + def test_help_with_command_should_show_help_page(self): """timew help with command should show help page""" code, out, err = self.t("help start") @@ -62,6 +74,12 @@ class TestHelp(TestCase): code, out2, err2 = self.t("track --help") self.assertEquals(out1, out2) + def test_command_with_help_short_option_should_show_help_page(self): + """timew command with -h should show help page""" + code, out1, err1 = self.t("help track") + code, out2, err2 = self.t("track -h") + self.assertEquals(out1, out2) + if __name__ == "__main__": from simpletap import TAPTestRunner