44
55import .cli
66import .parser_
7+ import .path_
78import .utils_
89import .ui
910import system
@@ -13,10 +14,7 @@ The 'help' command that can be executed on the root command.
1314
1415It finds the selected command and prints its help.
1516*/
16- help-command_ path / List arguments / List --invoked-command / string --ui / Ui :
17- // We are modifying the path, so make a copy.
18- path = path .copy
19-
17+ help-command_ path / Path arguments / List --ui / Ui :
2018 command / Command := path .last
2119
2220 for i := 0 ; i < arguments .size ; i ++:
@@ -32,25 +30,25 @@ help-command_ path/List arguments/List --invoked-command/string --ui/Ui:
3230 ui .abort "Unknown command: $ argument "
3331 unreachable
3432 command = subcommand
35- path . add command
33+ path += command
3634
37- emit-help_ path --invoked-command = invoked-command -- ui= ui
35+ emit-help_ path --ui = ui
3836
3937/**
4038Emits the help for the given command.
4139
4240The command is identified by the $path where the command is the last element.
4341*/
44- emit-help_ path / List --invoked-command / string --ui / Ui :
42+ emit-help_ path / Path --ui / Ui :
4543 ui .emit --kind = Ui .RESULT
4644 --structured =:
47- build-json-help_ path --invoked-command = invoked-command
45+ build-json-help_ path
4846 --text =:
49- generator := HelpGenerator path --invoked-command = invoked-command
47+ generator := HelpGenerator path
5048 generator .build-all
5149 generator .to-string
5250
53- build-json-help_ path / List --invoked-command / string -> Map :
51+ build-json-help_ path / Path -> Map :
5452 // Local block to build json objects for the given command.
5553 // Adds the converted json object to the out-map.
5654 extract-options := : | command / Command out-map / Map |
@@ -88,7 +86,8 @@ build-json-help_ path/List --invoked-command/string -> Map:
8886
8987 return {
9088 "name" : command .name ,
91- "path" : path .map : | command / Command | command .name ,
89+ "path" : path .commands .map : | command / Command | command .name ,
90+ "invoked-command" : path .invoked-command ,
9291 "help" : command .help_ ,
9392 "aliases" : command .aliases_ ,
9493 "options" : extract-options .call command { : } ,
@@ -105,14 +104,12 @@ The class also serves as a string builder for the `build_X` methods. The methods
105104*/
106105class HelpGenerator :
107106 command_ / Command
108- path_ / List
109- invoked-command_ / string
107+ path_ / Path
110108 buffer_ / List := [] // Buffered string.
111109 // The index in the buffer of the last separator.
112110 last-separator-pos_ / int := 0
113111
114- constructor .path_ --invoked-command / string :
115- invoked-command_ = invoked-command
112+ constructor .path_ :
116113 command_ = path_ .last
117114
118115 /**
@@ -181,7 +178,7 @@ class HelpGenerator:
181178 // They are sorted by name.
182179 // For the usage line we don't care for the short names of options.
183180
184- write_ invoked-command_ --indentation = indentation
181+ write_ path_ . invoked-command --indentation = indentation
185182 has-more-options := false
186183 for i := 0 ; i < path_ .size ; i ++:
187184 current-command / Command := path_ [ i ]
@@ -376,7 +373,7 @@ class HelpGenerator:
376373 if i != 0 : writeln_
377374 example-and-path := all-examples [ i ]
378375 example / Example := example-and-path [ 0 ]
379- example-path / List := example-and-path [ 1 ]
376+ example-path / Path := example-and-path [ 1 ]
380377
381378 build-example_ example --example-path = example-path
382379
@@ -389,22 +386,22 @@ class HelpGenerator:
389386 If $skip-first-level is true, then does not add the examples of this command, but only
390387 those of subcommands.
391388 */
392- add-global-examples_ command / Command all-examples / List --path / List --skip-first-level / bool= false -> none :
389+ add-global-examples_ command / Command all-examples / List --path / Path --skip-first-level / bool= false -> none :
393390 if not skip-first-level :
394391 global-examples := ( command .examples_ .filter : it .global-priority > 0 )
395392 all-examples .add-all ( global-examples .map : [ it , path ] )
396393
397394 command .subcommands_ .do : | subcommand / Command |
398395 if subcommand .is-hidden_ : continue .do
399- add-global-examples_ subcommand all-examples --path = ( path + [ subcommand ] )
396+ add-global-examples_ subcommand all-examples --path = ( path + subcommand )
400397
401398 /**
402399 Builds a single example.
403400
404401 The $example contains the description and arguments, and the $example-path is
405402 the path to the command that contains the example.
406403 */
407- build-example_ example / Example --example-path / List :
404+ build-example_ example / Example --example-path / Path :
408405 description := example .description .trim --right
409406 description-lines := ?
410407 if description .contains "\n " : description-lines = description .split "\n "
@@ -423,11 +420,11 @@ class HelpGenerator:
423420 Builds the example command line for the given $arguments-line.
424421 The command that defined the example is identified by the $example-path.
425422 */
426- build-example-command-line_ arguments-line / string --example-path / List :
423+ build-example-command-line_ arguments-line / string --example-path / Path :
427424 // Start by constructing a valid command line.
428425
429426 // The prefix consists of the subcommands.
430- prefix := example-path [ 1 .. ] .map : | command / Command | command .name
427+ prefix := example-path . commands [ 1 .. ] .map : | command / Command | command .name
431428 // Split the arguments line into individual arguments.
432429 // For example, `"foo --bar \"my password\""` is split into `["foo", "--bar", "my password"]`.
433430 example-arguments := split-arguments_ arguments-line
@@ -436,14 +433,14 @@ class HelpGenerator:
436433 // Parse it, to verify that it actually is valid.
437434 // We are also using the result to reorder the options.
438435 parser := Parser_ --invoked-command = "root" --for-help-example
439- invocation-path / List ? := null
436+ invocation-path / Path ? := null
440437 invocation-parameters / Parameters? := null
441438 exception := catch :
442439 parser .parse example-path .first command-line : | path parameters |
443440 invocation-path = path
444441 invocation-parameters = parameters
445442 if exception :
446- throw "Error in example '$ arguments-line ': $ exception "
443+ throw "Error in example '$ arguments-line ': $ exception. "
447444
448445 // For each command, collect the options that are defined on it and that were
449446 // used in the example.
@@ -528,8 +525,8 @@ class HelpGenerator:
528525 is-root := true
529526 // For examples, we don't want the full path that was used to invoke the
530527 // command (like `build/bin/artemis`), but only the basename.
531- app-name := basename_ invoked-command_
532- invocation-path .do : | current-command |
528+ app-name := basename_ example-path . invoked-command
529+ invocation-path .commands . do : | current-command |
533530 if is-root :
534531 is-root = false
535532 full-command .add app-name
0 commit comments