How to define subcommands for a `@main` command in a Julia app?

Since Julia now allows us to install an app/command to call it from the Shell by the @main function, I wonder how to mimic cases like some commands having subcommands in @main? Like git add, git status, git clone for git. That is, those subcommands should be gathered under a global name.

A possible solution I can think of is the check the second argument of ARGS, e.g., if it is a specific subcommand. I am not sure this is the correct way.

I was using Comonicon.jl, which allows subcommands as long as they are defined in submodules of a package.

I am not sure if I understand what you mean with subcommands but DocOpt.jl implements the infamous docopt concept (awesome talk by Vladimir Keleshev btw. https://youtu.be/pXhcPJK5cMc) and you can define a command line interface (CLI) pretty naturally: GitHub - docopt/DocOpt.jl: command line arguments parser

One of the first slides shows this CLI

and I assume the “subcommand” you are referring to would be something like tcp or serial in that example.

You would then need to parse the arguments and call the appropriate function.

If you however mean something like dedicated functions for different “subcommands”, like e.g. what Click for Python implements, I think there is nothing comparable (yet) in the Julia ecosystem. Note that Click provides type checking and direct mapping of functions to the CLI. Might be an overkill in some cases :wink:

The subcommand should be the first argument not the second argument, since the main script name does not appear at the beginning of ARGS.

Then you can ask the function corresponding to the subcommand to parse ARGS[2:end]. For example, ArgParse.jl can parse a user-supplied argument list.