Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve command type documentation #168

Open
wants to merge 3 commits into
base: 0.4.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 112 additions & 6 deletions docs/types/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,112 @@ command('usage pattern', 'help page'):
script
```

## Options and arguments (usage pattern)

`input.*` can also be used in expression filtered exec blocks (`|=`, see below).

### Arguments
```
command('cmd <required> [<optional>]'):
env:
REQUIRED: = input.argument('required')
OPTIONAL: = input.argument('optional')
exec: |
#!bash
echo "'${REQUIRED}'"
echo "'${OPTIONAL}'"
```
Defines arguments. The command will not be recognized without required arguments.

```
command('cmd %'):
env:
ALL: = input.argument('%')
exec: |
#!bash
echo "'${ALL}'"
```

The special token `%` describes an argument that contains
the rest of the input, it can only be the last character in the command.

### Arguments with select values

```
command('cmd [on|off]'):
env:
OPTIONAL_ARG: = input.command(1)
```
or
```
command('cmd (on|off])'):
env:
REQUIRED_ARG: = input.command(1)
```

Defines arguments that can only have select values, these only work correctly
as the last part in the command and must be read using `input.command(index)`, 0-based.

### Boolean options
```
command('cmd [--option]'):
env:
VERBATIM: = input.option('option')
YES_OR_NO: = input.option('option') ? 'yes' : 'no'
exec: |
#!bash
echo "'${VERBATIM}'"
echo "'${YES_OR_NO}'"
```

For a boolean option, `input.option` will read a `'1'` if given, `''` (empty string) otherwise.
The Inviqa base docker harness contains a convenient `boolToString()` function that provides
the yes/no conversion done here manually.

```
command('cmd [-iou]'):
env:
OPT_I: = input.option('i')
OPT_O: = input.option('o')
OPT_U: = input.option('u')
```

Sequence shortcut for defining multiple bool options.

### Value options
```
command('cmd [--option=<value>]'):
env:
AS_OPTION: = input.option('option')
...
```

### Mutually exclusive options
```
command('cmd [-b|--bool] [-s=<value>|--string=<value>]'):
env:
BOOL_OPTION: = input.option('bool') || input.option('b')
VALUE_OPTION: = input.option('string') ?: input.option('s')
exec: |
#!bash
echo "'${BOOL_OPTION}'"
echo "'${VALUE_OPTION}'"
```

You can use these to define mutually exclusive options including long/shortform alternatives.
Be aware these options are technically considered different options, so you have to read both if
you're using them for long/short alternatives.

### Technicalities

The console parser builder is capable of building loops like this:
```
command('cmd [-v=<value>]... (on|off)... <arg>...'):
```

However, in all of these cases, either workspace or the console itself errors out because arrays are passed
where strings are expected.

## Examples

### Hello world in bash
Expand All @@ -25,14 +131,14 @@ command('hello world'): |
echo "Hello World"
```

Only the usage pattern and body of the script are required, everything else is optional. Note the pipe after the header declaration to signify a multi-line string.
Only the usage pattern and body of the script are required, everything else is optional. Note the pipe after the header declaration to signify a multi-line YAML string.

### Using environment variables

```
command('hello from environment'):
env:
- MESSAGE: Hello World
MESSAGE: Hello World
exec: |
#!bash
echo "$MESSAGE"
Expand All @@ -58,8 +164,8 @@ attribute('aws'):

command('assets download'):
env:
- AWS_ID: =@('aws.id')
- AWS_KEY: =@('aws.key')
AWS_ID: =@('aws.id')
AWS_KEY: =@('aws.key')
exec: |
#!interpreter(workspace:/)|@
passthru ws-aws s3 sync @('aws.s3') assets/development
Expand All @@ -77,8 +183,8 @@ attribute('aws'):

command('assets download'):
env:
- AWS_ID: =@('aws.id')
- AWS_KEY: =@('aws.key')
AWS_ID: =@('aws.id')
AWS_KEY: =@('aws.key')
exec: |
#!interpreter(workspace:/)|=
passthru ws-aws s3 sync ={ @('aws.s3') ~ '/assets/development' } assets/development
Expand Down
Loading