Default subcommands in Core.Command

core

#1

Is there a way to specify a default subcommand in Core.Command? E.g., if I run main.native arg1 arg2 I would like to have the behavior main.native [default subcommand] arg1 arg2 if arg1 is not a valid subcommand. Instead, the default behavior is the help message and “unknown subcommand”:

$ ./main.native arg1 arg2
Description

  main.native SUBCOMMAND

=== subcommands ===

  foo        Do foo
  version    print version information
  help       explain a given subcommand (perhaps recursively)

unknown subcommand arg1

This is sensible in most cases. I just wonder if the interface makes any provisions for overriding this behavior (I couldn’t find any).

It looks like Command silently catches the Failure raised by unknown subcommand, so wrapping Command.run using try...with as a way to get around this doesn’t work.


#2

I believe the behavior you describe is simply not a feature of Core.Command, and I don’t think there’s a way of faking it.

I’m also not sure it’s a good idea. The behavior you describe seems inherently a little flaky and hard-to-predict, since adding a new subcommand can make old command-line invocations stop working. It seems like hell for shell-scripting contexts, for example.


#3

I agree in general it can introduce flakiness, so I don’t want to push too much on the interface. For such cases (like scripting contexts) it’s sensible to explicitly add subcommands. But sometimes you really want to just fall through to some default behavior (e.g., merlin does this).

I found a way to fake it though, and it’s not too ugly.

  • Check if the first arg is one of the known subcommands in Sys.argv
  • If not, inject the name of the default subcommand into the args list
  • Pass the args through the optional parameter with Command.run ~argv

Thanks!