Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
- [private-reveal](private-reveal#readme) - allowing users to reveal private commands, flags or environment variables
- [stdin](stdin#readme) - reading input from stdin
- [filters](filters#readme) - preventing commands from running unless custom conditions are met
- [argfile](argfile#readme) - auto-load arguments from a file
- [commands-expose](commands-expose#readme) - showing subcommands in the parent's help
- [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags
- [command-examples-on-error](command-examples-on-error#readme) - showing examples on error
Expand Down
2 changes: 2 additions & 0 deletions examples/argfile/.download
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--force
--log "some path with spaces.log"
1 change: 1 addition & 0 deletions examples/argfile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
download
86 changes: 86 additions & 0 deletions examples/argfile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Argfile Example

Demonstrates how to autoload additional arguments from a file using the
`argfile` command option.

This example was generated with:

```bash
$ bashly init --minimal
# ... now edit src/bashly.yml to match the example ...
# ... now create .download to match the example ...
$ bashly generate
```

<!-- include: .download -->

-----

## `bashly.yml`

````yaml
name: download
help: Sample application with autoloaded arguments
version: 0.1.0

# Allow users to configure args and flags in a file named '.download'
argfile: .download

args:
- name: source
required: true
help: URL to download from

flags:
- long: --force
short: -f
help: Overwrite existing files
- long: --log
short: -l
arg: path
help: Path to log file
````

## `.download`

````bash
--force
--log "some path with spaces.log"

````


## Output

### `$ ./download somesource`

````shell
# This file is located at 'src/root_command.sh'.
# It contains the implementation for the 'download' command.
# The code you write here will be wrapped by a function named 'root_command()'.
# Feel free to edit this file; your changes will persist when regenerating.
args:
- ${args[--force]} = 1
- ${args[--log]} = some path with spaces.log
- ${args[source]} = somesource


````

### `$ ./download somesource --log cli.log`

````shell
# This file is located at 'src/root_command.sh'.
# It contains the implementation for the 'download' command.
# The code you write here will be wrapped by a function named 'root_command()'.
# Feel free to edit this file; your changes will persist when regenerating.
args:
- ${args[--force]} = 1
- ${args[--log]} = cli.log
- ${args[source]} = somesource


````



20 changes: 20 additions & 0 deletions examples/argfile/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: download
help: Sample application with autoloaded arguments
version: 0.1.0

# Allow users to configure args and flags in a file named '.download'
argfile: .download

args:
- name: source
required: true
help: URL to download from

flags:
- long: --force
short: -f
help: Overwrite existing files
- long: --log
short: -l
arg: path
help: Path to log file
5 changes: 5 additions & 0 deletions examples/argfile/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
echo "# This file is located at 'src/root_command.sh'."
echo "# It contains the implementation for the 'download' command."
echo "# The code you write here will be wrapped by a function named 'root_command()'."
echo "# Feel free to edit this file; your changes will persist when regenerating."
inspect_args
12 changes: 12 additions & 0 deletions examples/argfile/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

rm -f ./download

set -x

bashly generate

### Try Me ###

./download somesource
./download somesource --log cli.log
1 change: 1 addition & 0 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def assert_command(key, value)
assert_optional_string "#{key}.group", value['group']
assert_optional_string "#{key}.filename", value['filename']
assert_optional_string "#{key}.function", value['function']
assert_optional_string "#{key}.argfile", value['argfile']

assert_boolean "#{key}.private", value['private']
assert_default_command "#{key}.default", value['default']
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/script/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Command < Base
class << self
def option_keys
@option_keys ||= %i[
alias args catch_all commands completions
alias argfile args catch_all commands completions
default dependencies environment_variables examples
extensible expose filename filters flags
footer function group help help_header_override name
Expand Down
5 changes: 5 additions & 0 deletions lib/bashly/script/introspection/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def catch_all_used_anywhere?
deep_commands(include_self: true).any? { |x| x.catch_all.enabled? }
end

# Returns true if the command or any of its descendants has `argfile`
def argfile_used_anywhere?
deep_commands(include_self: true).any?(&:argfile)
end

# Returns a full list of the Command names and aliases combined
def command_aliases
commands.map(&:aliases).flatten
Expand Down
5 changes: 5 additions & 0 deletions lib/bashly/views/command/argfile_filter.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= view_marker

> load_command_argfile "{{ argfile }}" "$@"
> set -- "${argfile_input[@]}"
>
36 changes: 36 additions & 0 deletions lib/bashly/views/command/argfile_helpers.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
= view_marker

> load_command_argfile() {
> local argfile_path line arg
> argfile_path="$1"
> shift
> argfile_input=()
>
> if [[ ! -f "$argfile_path" ]]; then
> argfile_input=("$@")
> return
> fi
>
> while IFS= read -r line || [[ -n "$line" ]]; do
> line="${line#"${line%%[![:space:]]*}"}"
> line="${line%"${line##*[![:space:]]}"}"
>
> [[ -z "$line" || "${line:0:1}" == "#" ]] && continue
>
> if [[ "$line" =~ ^(-{1,2}[^[:space:]]+)[[:space:]]+(.+)$ ]]; then
> for arg in "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"; do
> arg="${arg#"${arg%%[![:space:]]*}"}"
> arg="${arg%"${arg##*[![:space:]]}"}"
> [[ "$arg" =~ ^\"(.*)\"$ || "$arg" =~ ^\'(.*)\'$ ]] && arg="${BASH_REMATCH[1]}"
> argfile_input+=("$arg")
> done
> else
> arg="$line"
> [[ "$arg" =~ ^\"(.*)\"$ || "$arg" =~ ^\'(.*)\'$ ]] && arg="${BASH_REMATCH[1]}"
> argfile_input+=("$arg")
> fi
> done <"$argfile_path"
>
> argfile_input+=("$@")
> }
>
1 change: 1 addition & 0 deletions lib/bashly/views/command/master_script.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
= render :version_command
= render :usage
= render :normalize_input
= render :argfile_helpers if argfile_used_anywhere?
= render :inspect_args if Settings.enabled? :inspect_args
= render :user_lib if user_lib.any?
= render :command_functions
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/views/command/parse_requirements.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
> local key
>

= render(:argfile_filter).indent 2 if argfile
= render(:fixed_flags_filter).indent 2
= render(:environment_variables_filter).indent 2
= render(:dependencies_filter).indent 2
Expand Down
15 changes: 15 additions & 0 deletions schemas/bashly.json
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,15 @@
"dir_commands/list.sh"
]
},
"argfile-property": {
"title": "argfile",
"description": "A file containing additional arguments to autoload for the current script or sub-command\nhttps://bashly.dev/configuration/command/#argfile",
"type": "string",
"minLength": 1,
"examples": [
".mycli"
]
},
"filters-property": {
"title": "filters",
"description": "Filters of the current script or sub-command\nhttps://bashly.dev/configuration/command/#filters",
Expand Down Expand Up @@ -951,6 +960,9 @@
"filename": {
"$ref": "#/definitions/filename-property"
},
"argfile": {
"$ref": "#/definitions/argfile-property"
},
"filters": {
"$ref": "#/definitions/filters-property"
},
Expand Down Expand Up @@ -1028,6 +1040,9 @@
"filename": {
"$ref": "#/definitions/filename-property"
},
"argfile": {
"$ref": "#/definitions/argfile-property"
},
"filters": {
"$ref": "#/definitions/filters-property"
},
Expand Down
23 changes: 23 additions & 0 deletions spec/approvals/examples/argfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
+ bashly generate
creating user files in src
skipped src/root_command.sh (exists)
created ./download
run ./download --help to test your bash script
+ ./download somesource
# This file is located at 'src/root_command.sh'.
# It contains the implementation for the 'download' command.
# The code you write here will be wrapped by a function named 'root_command()'.
# Feel free to edit this file; your changes will persist when regenerating.
args:
- ${args[--force]} = 1
- ${args[--log]} = some path with spaces.log
- ${args[source]} = somesource
+ ./download somesource --log cli.log
# This file is located at 'src/root_command.sh'.
# It contains the implementation for the 'download' command.
# The code you write here will be wrapped by a function named 'root_command()'.
# Feel free to edit this file; your changes will persist when regenerating.
args:
- ${args[--force]} = 1
- ${args[--log]} = cli.log
- ${args[source]} = somesource
13 changes: 13 additions & 0 deletions support/schema/bashly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,15 @@ definitions:
minLength: 1
examples:
- dir_commands/list.sh
argfile-property:
title: argfile
description: |-
A file containing additional arguments to autoload for the current script or sub-command
https://bashly.dev/configuration/command/#argfile
type: string
minLength: 1
examples:
- .mycli
filters-property:
title: filters
description: |-
Expand Down Expand Up @@ -802,6 +811,8 @@ definitions:
$ref: '#/definitions/sub-command-expose-property'
filename:
$ref: '#/definitions/filename-property'
argfile:
$ref: '#/definitions/argfile-property'
filters:
$ref: '#/definitions/filters-property'
function:
Expand Down Expand Up @@ -850,6 +861,8 @@ properties:
$ref: '#/definitions/root-extensible-property'
filename:
$ref: '#/definitions/filename-property'
argfile:
$ref: '#/definitions/argfile-property'
filters:
$ref: '#/definitions/filters-property'
function:
Expand Down
Loading