Command line parsing for Emacs.
Add commander to your Cask file:
(depends-on "commander")- commander
(&rest forms) - command
(&rest args) - option
(&rest args) - name
(name) - description
(description) - default
(command-or-function &rest arguments) - parse
(arguments)
Define schema within this block.
(commander
;; schema here
)Define a command.
- First argument is the name of the command.
- Second argument either:
- a description of the command
- the command function (the function doc-string will be used as description)
- Third argument is command function if description is specified
- Rest of
argsare command default values
Define the command foo with no arguments.
(commander
(command "foo" "Foo" fn))$ emacs -Q -- fooDefine the command foo with a required argument.
(commander
(command "foo <bar>" "Foo" fn))$ emacs -Q -- foo barDefine the command foo with an optional argument. If argument is not
specified, "baz" will be used as the argument value.
(commander
(command "foo [bar]" "Foo" fn "baz"))$ emacs -Q -- foo
$ emacs -Q -- foo quxDefine the command foo with at least one required argument.
(commander
(command "foo <*>" "Foo" fn))$ emacs -Q -- foo bar
$ emacs -Q -- foo bar baz quxDefine the command foo with zero or more arguments.
(commander
(command "foo [*]" "Foo" fn))$ emacs -Q -- foo
$ emacs -Q -- foo bar baz quxDefine the command foo without description. The foo doc-string
will be used as description.
(defun foo ()
"Return FOO.")
(commander
(default commander-print-usage-and-exit)
(command "foo" foo))$ emacs -QDefine an option.
- First argument is the flags.
- Second argument either:
- a description of the option
- the option function (the function doc-string will be used as description)
- Third argument is option function if description is specified
- Rest of
argsare option default values
Define the option --foo with no arguments.
(commander
(option "--foo" "Foo" fn))$ emacs -Q -- --fooDefine the option --foo with a required argument.
(commander
(option "--foo <bar>" "Foo" fn))$ emacs -Q -- --foo barDefine the option --foo with an optional argument. If argument is not
specified, "baz" will be used as the argument value.
(commander
(option "--foo [bar]" "Foo" fn "baz"))$ emacs -Q -- --foo
$ emacs -Q -- --foo quxDefine the option --foo with at least one required argument.
(commander
(option "--foo <*>" "Foo" fn))$ emacs -Q -- --foo bar
$ emacs -Q -- --foo bar baz quxDefine the option --foo with zero or more arguments.
(commander
(option "--foo [*]" "Foo" fn))$ emacs -Q -- --foo
$ emacs -Q -- --foo bar baz quxDefine the option --foo with with an alias -f.
(commander
(option "--foo, -f" "Foo" fn))$ emacs -Q -- --foo
$ emacs -Q -- -fDefine the option --foo without description. The foo doc-string
will be used as description.
(defun foo ()
"Return FOO.")
(commander
(default commander-print-usage-and-exit)
(option "--foo" foo))$ emacs -QSpecify name in usage information.
Define the option --help that prints usage information with
my-awesome-program as program name.
(commander
(name "my-awesome-program")
(option "--help" "Show usage information" commander-print-usage))$ emacs -Q -- --helpSpecify description in usage information.
Define the option --help that prints usage information with description.
(commander
(name "my-awesome-program")
(description "Truly awesome program, does what you wish for")
(option "--help" "Show usage information" commander-print-usage))$ emacs -Q -- --helpSpecify default behaviour when no matching commands.
If command-or-function is a string, use that command if no command
is specified. If symbol, call that function with all arguments if
first argument does not match a command.
Define two commands show and hide and make show the default with
everyone as argument.
(commander
(default "show" "everyone")
(command "show <stuff>" "Show stuff" show)
(command "hide <stuff>" "Hide stuff" hide))$ emacs -Q -- show me
$ emacs -Q -- hide you
$ emacs -QFor each file argument, print the content.
(defun print-file-content (file)
(princ (f-read file)))
(commander
(default print-file-content "foo.txt"))$ emacs -Q -- foo.txt bar.txt
$ emacs -QParse file for default arguments and command. Each line is a command
or option, including arguments.
For each file argument, print the content.
;; cmd.opts
;;
;; --foo bar
(commander
(config "cmd.opts")
(option "--foo <arg>" "..." ignore)
(option "--bar <arg>" "..." ignore))$ emacs -Q -- --bar arg
$ emacs -QParse arguments with defined schema. If #parse is not called
explicitly, it is done automatically with commander-args first and
if that's not present, it is called with the value of (cdr command-line-args-left).
(commander
;; schema
(parse some-custom-variable)
)Create a new project, with optional dev mode:
;; emacs -Q -- create
;; emacs -Q -- create --dev
(commander
(command "create" "Create new project" create)
(option "--dev" "Run command in dev mode" dev-mode))Simple find task:
;; emacs -Q -- find
;; emacs -Q -- find path/to/dir --name 'foo.el' --type f
(commander
(command "find [path]" "Find stuff" find)
(option "--name <path>" "Specify file name" name)
(option "--type <type>" "Specify file type" type))Automatic usage information (note that if description is not specified, the function doc-string is used as description):
(commander
(command "find [path]" "Find stuff" find)
(command "help" "Show usage information" commander-print-usage)
(option "--name <path>" "Specify file name" name)
(option "--type <type>" "Specify file type" type))The command emacs -Q -- help will print:
USAGE: find COMMAND [OPTIONS]
COMMANDS:
helpShow usage information
findFind stuff
OPTIONS:
--type Specify file type
--name Specify file name
For more examples, check out: https://github.com/rejeep/commander.el/tree/master/examples
Contribution is much welcome!
Install cask if you haven't already, then:
$ cd /path/to/commander.el
$ caskRun all tests with:
$ make