Command Arguments
Thursday, July 10, 2025
A question was asked recently on the Factor mailing list about the Argument Parser that I had previously implemented in Factor:
I have been trying to hack on command-line.parser to add the ability to call it with commands.
The specific feature they want is similar to the ArgumentParser.add_subparsers function in Python’s argparse module. I spent a little bit of time thinking about a quick implementation that can get us started, and applied this patch to support commands.
Here’s their example of a MAIN with two commands with different options using with-commands:
MAIN: [
H{
{
"add"
{
T{ option
{ name "a" }
{ type integer }
{ #args 1 }
}
}
}
{
"subtract"
{
T{ option
{ name "s" }
{ type integer }
{ #args 1 }
}
}
}
} [ ] with-commands
]
We currently produce no output by default when no command is specified:
$ ./factor foo.factor
The default help prints the possible commands:
$ ./factor foo.factor --help
Usage:
factor foo.factor [--help] [command]
Arguments:
command {add,subtract}
Options:
--help show this help and exit
Or get default help for a command:
$ ./factor foo.factor add --help
Usage:
factor foo.factor add [--help] [a]
Arguments:
a
Options:
--help show this help and exit
Or print an error if the argument is not a valid command:
$ ./factor foo.factor multiply
ERROR: Invalid value 'multiply' for option 'command'
There are other features we might want to add to this including per-command
metadata with a brief description of the command, support for additional
top-level options besides just the command
, and perhaps a different way of
handling the no command case rather than empty output.
This is available in the latest developer version!