Documentation
PLEASE NOTE: This document applies to v2.9 version and not to the latest stable release v2.16
Documentation for other releases can be found by using the version selector in the left bottom of any doc page.Command-line interfaces
rash
includes a powerful command-line argument parser based on the documentation in your script.
Rather than writing complex argument parsing code, you simply document how your script should be used,
and rash
handles parsing the arguments according to that specification.
This implementation is inspired by Docopt, a command-line interface description language. The core philosophy is: “Program’s help message is the source of truth for command-line argument parsing logic.”
Why use document-based argument parsing?
- Documentation first: You write the help text that users will see, not abstract parsing rules
- Single source of truth: No risk of documentation being out of sync with the code
- Declarative: Describe what the interface looks like, not how to parse it
- Complete: Supports complex command-line interfaces with commands, options, and arguments
Basic example
Here’s a simple example of how it works:
#!/usr/bin/env -S rash --
#
# Copy files from source to dest dir
#
# Usage:
# copy.rh [options] <source>... <dest>
# copy.rh
#
# Options:
# -h --help show this help message and exit
# --mode MODE dest file permissions [default: 0644]
- copy:
src: "{{ item }}"
dest: "{{ dest }}/{{ item | split('/') | last }}"
mode: "{{ options.mode }}"
loop: "{{ source | default([]) }}"
In this example:
- The usage pattern describes the command-line interface
- The arguments are automatically parsed and made available as variables
- No additional parsing code is needed
Format specification
The docopt format uses a specific syntax to define your interface:
- Begin with a
#!/usr/bin/env rash
shebang line - Include a comment section starting with
#
- Provide a
Usage:
section that defines the command-line patterns - Optionally include
Options:
,Arguments:
, or other sections for more details
When users invoke your script, rash
will:
- Parse the command-line arguments according to your usage patterns
- Make the parsed values available as variables in your script
- Display the help text if requested with
--help
or if invalid arguments are provided
For a complete reference of the syntax, see the Syntax section, and for details on how arguments are parsed and made available to your script, see the Parser section.