kotlinx-cli icon indicating copy to clipboard operation
kotlinx-cli copied to clipboard

Show help if no subcommands provided

Open sviridov-alexey opened this issue 4 years ago • 9 comments

It would be nice to have an option to show the help of the subcommand if no options and no subcommands are specified.

sviridov-alexey avatar Apr 27 '21 04:04 sviridov-alexey

Please, could you clarify? If there are subcommands you would like to get help message for each of these subcommands?

If we have test command and subcommands a, b, c. Then you want to have an option test --help-detailed that shows not only list of subcommands, but all flags for each of a, b, c?

LepilkinaElena avatar Apr 27 '21 05:04 LepilkinaElena

Not exactly, I want to get same effect like "-h" option when no any option is given. I.e.

command subcommand

should act like

command subcommand -h

sviridov-alexey avatar Apr 27 '21 05:04 sviridov-alexey

This is replated to subcommand with only possible subcommand, without arguments and options

sviridov-alexey avatar Apr 27 '21 06:04 sviridov-alexey

But if subcommand can be executed without any options? It can have all options with default values.

LepilkinaElena avatar Apr 27 '21 06:04 LepilkinaElena

But if subcommand can be executed without any options? It can have all options with default values.

Generally speaking yes, but on the other hand, there isn't any possibility to show help for such "umbrella" subcommand, because makeUsage is internal, same as printAndTerminate. Also, I can't detect in execute method, was nested sumcommand present or not

sviridov-alexey avatar Apr 27 '21 06:04 sviridov-alexey

this would easily be achieved by just making makeUsage public. So that those who need to print usage when subcommand has not been provided should just call print(makeUsage()) and terminate the app.

andylamax avatar Jul 31 '22 23:07 andylamax

Just stumbled upon the same issue. Would be nice if there would be an option to enable this behavior or the option to call makeUsage. However, I worked around the issue by just adding -h to the arguments if the list is empty. Of course this only works if you don't want to use it on a subcommand and you know that 0 arguments is invalid.

    val arguments = "-h"
        .takeIf { args.isEmpty() }
        ?.let { args + it }
        ?: args

    parser.parse(arguments)

jodoll avatar Aug 17 '22 15:08 jodoll

Another vote for making printError or makeUsage public. I'd like to be able to output an error message and the usage statement if the app is just executed with no subcommands provided.

lkb2k avatar Dec 02 '22 17:12 lkb2k

Just stumbled upon the same issue. Would be nice if there would be an option to enable this behavior or the option to call makeUsage. However, I worked around the issue by just adding -h to the arguments if the list is empty. Of course this only works if you don't want to use it on a subcommand and you know that 0 arguments is invalid.

    val arguments = "-h"
        .takeIf { args.isEmpty() }
        ?.let { args + it }
        ?: args

    parser.parse(arguments)

Oh, nice idea! I tried it but unfortunately even this workaround isn't great since it will cause the app to return exit code 0 as if it was invoked with -h on purpose rather than returning an error.

lkb2k avatar Dec 02 '22 17:12 lkb2k