diff --git a/src/help-generator_.toit b/src/help-generator_.toit index 9e30eb9..7ae18ab 100644 --- a/src/help-generator_.toit +++ b/src/help-generator_.toit @@ -5,6 +5,7 @@ import .cli import .parser_ import .utils_ +import system /** The 'help' command that can be executed on the root command. @@ -478,8 +479,21 @@ class HelpGenerator: // Reconstruct the full command line, but now with the options next to the // commands that defined them. full-command := [] + is-root := true + // For examples we don't want the full path that was used to invoke the + // command (like `build/bin/artemis`), but only the basename. + app-name := invoked-command_ + separator-index := app-name.index-of --last "/" + if system.platform == system.PLATFORM-WINDOWS: + separator-index = max separator-index (app-name.index-of --last "\\") + app-name = app-name[separator-index + 1..] + parsed-path.do: | current-command | - full-command.add current-command.name + if is-root: + is-root = false + full-command.add app-name + else: + full-command.add current-command.name command-options := options-for-command.get current-command if command-options: command-options.do: | option/string | diff --git a/tests/help_test.toit b/tests/help_test.toit index 2913693..e692c8a 100644 --- a/tests/help_test.toit +++ b/tests/help_test.toit @@ -56,7 +56,7 @@ test-combination: Two lines. Usage: - root [] [--] + bin/app [] [--] Options: -h, --help Show help for this command. @@ -67,11 +67,11 @@ test-combination: Examples: # Example 1: - root --option1 foo rest + app --option1 foo rest """ check-output cmd-help: | ui/cli.Ui | - cmd.run ["--help"] --ui=ui --invoked-command="root" - expect-equals cmd-help (cmd.help --invoked-command="root") + cmd.run ["--help"] --ui=ui --invoked-command="bin/app" + expect-equals cmd-help (cmd.help --invoked-command="bin/app") sub := cli.Command "sub" --aliases=["sss"] @@ -100,7 +100,7 @@ test-combination: Two lines. Usage: - root [] + bin/app [] Commands: help Show help for a command. @@ -112,21 +112,21 @@ test-combination: Examples: # Full example: - root --option1 root sub + app --option1 root sub # Sub Example 2: - root --option1 foo sub --option_sub1='xyz' + app --option1 foo sub --option_sub1='xyz' """ check-output cmd-help: | ui/cli.Ui | - cmd.run ["--help"] --ui=ui --invoked-command="root" + cmd.run ["--help"] --ui=ui --invoked-command="bin/app" - expect-equals cmd-help (cmd.help --invoked-command="root") + expect-equals cmd-help (cmd.help --invoked-command="bin/app") sub-help := """ Long sub. Usage: - root sub [] + bin/app sub [] Aliases: sss @@ -141,18 +141,18 @@ test-combination: Examples: # Sub Example 1: - root sub + app sub # Sub Example 2: - root --option1 foo sub --option_sub1='xyz' + app --option1 foo sub --option_sub1='xyz' """ check-output sub-help: | ui/cli.Ui | - cmd.run ["help", "sub"] --ui=ui --invoked-command="root" + cmd.run ["help", "sub"] --ui=ui --invoked-command="bin/app" test-usage: build-usage := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-usage help.to-string @@ -171,11 +171,11 @@ test-usage: expected-usage := """ Usage: - root --option1= --option2= [] [--] [] [...] + bin/app --option1= --option2= [] [--] [] [...] """ actual-usage := build-usage.call [cmd] expect-equals expected-usage actual-usage - expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="root")\n" + expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="bin/app")\n" // Test different types. cmd = cli.Command "root" @@ -195,11 +195,11 @@ test-usage: // Since there are named options that aren't shown, there is a []. expected-usage = """ Usage: - root --option1= --option2= --option3= --option4 --option5= [] + bin/app --option1= --option2= --option3= --option4 --option5= [] """ actual-usage = build-usage.call [cmd] expect-equals expected-usage actual-usage - expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="root")\n" + expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="bin/app")\n" cmd = cli.Command "root" --options=[ @@ -211,11 +211,11 @@ test-usage: // If all options are required, there is no []. expected-usage = """ Usage: - root --option1= --option2= + bin/app --option1= --option2= """ actual-usage = build-usage.call [cmd] expect-equals expected-usage actual-usage - expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="root")\n" + expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="bin/app")\n" // Test the same options as rest arguments. cmd = cli.Command "root" @@ -234,11 +234,11 @@ test-usage: // Also, optional arguments are shown. expected-usage = """ Usage: - root [--] [] + bin/app [--] [] """ actual-usage = build-usage.call [cmd] expect-equals expected-usage actual-usage - expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="root")\n" + expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="bin/app")\n" cmd = cli.Command "root" --options=[ @@ -259,7 +259,7 @@ test-usage: // All options must still be sorted. expected-usage = """ Usage: - root --option1= --option3 sub --sub-option1= --sub-option3 [] + bin/app --option1= --option3 sub --sub-option1= --sub-option3 [] """ actual-usage = build-usage.call [cmd, sub] expect-equals expected-usage actual-usage @@ -276,11 +276,11 @@ test-usage: """ actual-usage = build-usage.call [cmd] expect-equals expected-usage actual-usage - expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="root")\n" + expect-equals expected-usage "Usage:\n $(cmd.usage --invoked-command="bin/app")\n" test-aliases: build-aliases := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-aliases help.to-string @@ -316,7 +316,7 @@ test-aliases: test-commands: build-commands := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-commands help.to-string @@ -445,12 +445,12 @@ test-commands: test-options: build-local-options := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-local-options help.to-string build-global-options := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-global-options help.to-string @@ -614,7 +614,7 @@ test-options: test-examples: build-examples := : | path/List | - help := HelpGenerator path --invoked-command="root" + help := HelpGenerator path --invoked-command="bin/app" help.build-examples help.to-string @@ -638,12 +638,12 @@ test-examples: expected := """ Examples: # Example 1: - root --option1=499 + app --option1=499 # Example 2 # over multiple lines: - root --option1=1 - root --option1=2 + app --option1=1 + app --option1=2 """ actual := build-examples.call [cmd] expect-equals expected actual @@ -699,33 +699,33 @@ test-examples: expected = """ Examples: # Example 1: - root --option1=499 sub subsub + app --option1=499 sub subsub # Example 2: - root sub --option_sub1=499 subsub + app sub --option_sub1=499 subsub # Example 3: - root sub subsub --option_subsub1=499 + app sub subsub --option_subsub1=499 # Example 4: - root --option1=499 sub --option_sub1=499 subsub --option_subsub1=499 + app --option1=499 sub --option_sub1=499 subsub --option_subsub1=499 # Example 5: - root --option1 499 sub --option_sub1 499 subsub --option_subsub1 499 + app --option1 499 sub --option_sub1 499 subsub --option_subsub1 499 # Example 6 with rest: - root --option1 499 sub --option_sub1 499 subsub --option_subsub1 499 rest1 rest2 + app --option1 499 sub --option_sub1 499 subsub --option_subsub1 499 rest1 rest2 # Clusters go to the first that accepts all: - root sub -ab subsub -c - root -a sub -b subsub -c - root sub -b subsub -ca - root -a sub subsub -bc + app sub -ab subsub -c + app -a sub -b subsub -c + app sub -b subsub -ca + app -a sub subsub -bc # short_names can also have additional params: - root sub -ay 33 subsub -c - root sub -b subsub -cx 42 - root -a sub subsub -bz 55 + app sub -ay 33 subsub -c + app sub -b subsub -cx 42 + app -a sub subsub -bz 55 """ actual = build-examples.call [cmd, sub, sub2] expect-equals expected actual @@ -764,22 +764,22 @@ test-examples: expected = """ Examples: # Root example 1: - root sub + app sub # Root example 2: - root sub2 + app sub2 # Example 5: - root sub2 global5 + app sub2 global5 # Example 1: - root sub global3 + app sub global3 # Example 3: - root sub global1 + app sub global1 # Example 6: - root sub2 global1 + app sub2 global1 """ actual = build-examples.call [cmd] expect-equals expected actual