Skip to content

Commit 1198141

Browse files
authored
Merge pull request #703 from bashly-framework/add/argfile-support
Add support for loading args from argfiles
2 parents 7786cd2 + 0689d08 commit 1198141

File tree

17 files changed

+228
-1
lines changed

17 files changed

+228
-1
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
3939
- [private-reveal](private-reveal#readme) - allowing users to reveal private commands, flags or environment variables
4040
- [stdin](stdin#readme) - reading input from stdin
4141
- [filters](filters#readme) - preventing commands from running unless custom conditions are met
42+
- [argfile](argfile#readme) - auto-load arguments from a file
4243
- [commands-expose](commands-expose#readme) - showing subcommands in the parent's help
4344
- [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags
4445
- [command-examples-on-error](command-examples-on-error#readme) - showing examples on error

examples/argfile/.download

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--force
2+
--log "some path with spaces.log"

examples/argfile/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download

examples/argfile/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Argfile Example
2+
3+
Demonstrates how to autoload additional arguments from a file using the
4+
`argfile` command option.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init --minimal
10+
# ... now edit src/bashly.yml to match the example ...
11+
# ... now create .download to match the example ...
12+
$ bashly generate
13+
```
14+
15+
<!-- include: .download -->
16+
17+
-----
18+
19+
## `bashly.yml`
20+
21+
````yaml
22+
name: download
23+
help: Sample application with autoloaded arguments
24+
version: 0.1.0
25+
26+
# Allow users to configure args and flags in a file named '.download'
27+
argfile: .download
28+
29+
args:
30+
- name: source
31+
required: true
32+
help: URL to download from
33+
34+
flags:
35+
- long: --force
36+
short: -f
37+
help: Overwrite existing files
38+
- long: --log
39+
short: -l
40+
arg: path
41+
help: Path to log file
42+
````
43+
44+
## `.download`
45+
46+
````bash
47+
--force
48+
--log "some path with spaces.log"
49+
50+
````
51+
52+
53+
## Output
54+
55+
### `$ ./download somesource`
56+
57+
````shell
58+
# This file is located at 'src/root_command.sh'.
59+
# It contains the implementation for the 'download' command.
60+
# The code you write here will be wrapped by a function named 'root_command()'.
61+
# Feel free to edit this file; your changes will persist when regenerating.
62+
args:
63+
- ${args[--force]} = 1
64+
- ${args[--log]} = some path with spaces.log
65+
- ${args[source]} = somesource
66+
67+
68+
````
69+
70+
### `$ ./download somesource --log cli.log`
71+
72+
````shell
73+
# This file is located at 'src/root_command.sh'.
74+
# It contains the implementation for the 'download' command.
75+
# The code you write here will be wrapped by a function named 'root_command()'.
76+
# Feel free to edit this file; your changes will persist when regenerating.
77+
args:
78+
- ${args[--force]} = 1
79+
- ${args[--log]} = cli.log
80+
- ${args[source]} = somesource
81+
82+
83+
````
84+
85+
86+

examples/argfile/src/bashly.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: download
2+
help: Sample application with autoloaded arguments
3+
version: 0.1.0
4+
5+
# Allow users to configure args and flags in a file named '.download'
6+
argfile: .download
7+
8+
args:
9+
- name: source
10+
required: true
11+
help: URL to download from
12+
13+
flags:
14+
- long: --force
15+
short: -f
16+
help: Overwrite existing files
17+
- long: --log
18+
short: -l
19+
arg: path
20+
help: Path to log file
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
echo "# This file is located at 'src/root_command.sh'."
2+
echo "# It contains the implementation for the 'download' command."
3+
echo "# The code you write here will be wrapped by a function named 'root_command()'."
4+
echo "# Feel free to edit this file; your changes will persist when regenerating."
5+
inspect_args

examples/argfile/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f ./download
4+
5+
set -x
6+
7+
bashly generate
8+
9+
### Try Me ###
10+
11+
./download somesource
12+
./download somesource --log cli.log

lib/bashly/config_validator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def assert_command(key, value)
193193
assert_optional_string "#{key}.group", value['group']
194194
assert_optional_string "#{key}.filename", value['filename']
195195
assert_optional_string "#{key}.function", value['function']
196+
assert_optional_string "#{key}.argfile", value['argfile']
196197

197198
assert_boolean "#{key}.private", value['private']
198199
assert_default_command "#{key}.default", value['default']

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Command < Base
1414
class << self
1515
def option_keys
1616
@option_keys ||= %i[
17-
alias args catch_all commands completions
17+
alias argfile args catch_all commands completions
1818
default dependencies environment_variables examples
1919
extensible expose filename filters flags
2020
footer function group help help_header_override name

lib/bashly/script/introspection/commands.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ def catch_all_used_anywhere?
77
deep_commands(include_self: true).any? { |x| x.catch_all.enabled? }
88
end
99

10+
# Returns true if the command or any of its descendants has `argfile`
11+
def argfile_used_anywhere?
12+
deep_commands(include_self: true).any?(&:argfile)
13+
end
14+
1015
# Returns a full list of the Command names and aliases combined
1116
def command_aliases
1217
commands.map(&:aliases).flatten

0 commit comments

Comments
 (0)