Skip to content

Commit 3363442

Browse files
authored
Merge pull request #704 from bashly-framework/fix/argfile
Change `command.argfile` to apply defaults instead of prepending the command line
2 parents 2fca56b + 47aa242 commit 3363442

File tree

16 files changed

+172
-52
lines changed

16 files changed

+172
-52
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +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
42+
- [argfile](argfile#readme) - auto-load flag defaults from a file
4343
- [commands-expose](commands-expose#readme) - showing subcommands in the parent's help
4444
- [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags
4545
- [command-examples-on-error](command-examples-on-error#readme) - showing examples on error

examples/argfile/.download

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
# Boolean flags in the argfile are loaded as defaults
12
--force
3+
4+
# Flag values must appear on the same line
25
--log "some path with spaces.log"
6+
7+
# Arguments in argfile also work for repeatable flags
8+
--header "x-from-file: 1"
9+
10+
# Unknown flags in the argfile are ignored
11+
--no-such-flag
12+
13+
# Non-flag lines in the argfile are ignored
14+
this line is ignored

examples/argfile/README.md

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Argfile Example
22

3-
Demonstrates how to autoload additional arguments from a file using the
4-
`argfile` command option.
3+
Demonstrates how to autoload flag defaults from a file using the `argfile`
4+
command option.
55

66
This example was generated with:
77

@@ -23,7 +23,7 @@ name: download
2323
help: Sample application with autoloaded arguments
2424
version: 0.1.0
2525

26-
# Allow users to configure args and flags in a file named '.download'
26+
# Allow users to configure flag defaults in a file named '.download'
2727
argfile: .download
2828

2929
args:
@@ -39,19 +39,47 @@ flags:
3939
short: -l
4040
arg: path
4141
help: Path to log file
42+
43+
# Arguments in argfile also work for repeatable and unique flags
44+
- long: --header
45+
short: -H
46+
arg: value
47+
repeatable: true
48+
unique: true
49+
help: Add an HTTP header
4250
````
4351

4452
## `.download`
4553

4654
````bash
55+
# Boolean flags in the argfile are loaded as defaults
4756
--force
57+
58+
# Flag values must appear on the same line
4859
--log "some path with spaces.log"
4960

61+
# Arguments in argfile also work for repeatable flags
62+
--header "x-from-file: 1"
63+
64+
# Unknown flags in the argfile are ignored
65+
--no-such-flag
66+
67+
# Non-flag lines in the argfile are ignored
68+
this line is ignored
69+
5070
````
5171

5272

5373
## Output
5474

75+
### `$ ./download --version`
76+
77+
````shell
78+
0.1.0
79+
80+
81+
````
82+
5583
### `$ ./download somesource`
5684

5785
````shell
@@ -61,6 +89,7 @@ flags:
6189
# Feel free to edit this file; your changes will persist when regenerating.
6290
args:
6391
- ${args[--force]} = 1
92+
- ${args[--header]} = x-from-file:\ 1
6493
- ${args[--log]} = some path with spaces.log
6594
- ${args[source]} = somesource
6695

@@ -76,11 +105,44 @@ args:
76105
# Feel free to edit this file; your changes will persist when regenerating.
77106
args:
78107
- ${args[--force]} = 1
108+
- ${args[--header]} = x-from-file:\ 1
79109
- ${args[--log]} = cli.log
80110
- ${args[source]} = somesource
81111

82112

83113
````
84114

115+
### `$ ./download somesource --header "x-from-cli: 2"`
116+
117+
````shell
118+
# This file is located at 'src/root_command.sh'.
119+
# It contains the implementation for the 'download' command.
120+
# The code you write here will be wrapped by a function named 'root_command()'.
121+
# Feel free to edit this file; your changes will persist when regenerating.
122+
args:
123+
- ${args[--force]} = 1
124+
- ${args[--header]} = x-from-file:\ 1 x-from-cli:\ 2
125+
- ${args[--log]} = some path with spaces.log
126+
- ${args[source]} = somesource
127+
128+
129+
````
130+
131+
### `$ ./download somesource --header "x-from-file: 1"`
132+
133+
````shell
134+
# This file is located at 'src/root_command.sh'.
135+
# It contains the implementation for the 'download' command.
136+
# The code you write here will be wrapped by a function named 'root_command()'.
137+
# Feel free to edit this file; your changes will persist when regenerating.
138+
args:
139+
- ${args[--force]} = 1
140+
- ${args[--header]} = x-from-file:\ 1
141+
- ${args[--log]} = some path with spaces.log
142+
- ${args[source]} = somesource
143+
144+
145+
````
146+
85147

86148

examples/argfile/src/bashly.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: download
22
help: Sample application with autoloaded arguments
33
version: 0.1.0
44

5-
# Allow users to configure args and flags in a file named '.download'
5+
# Allow users to configure flag defaults in a file named '.download'
66
argfile: .download
77

88
args:
@@ -18,3 +18,11 @@ flags:
1818
short: -l
1919
arg: path
2020
help: Path to log file
21+
22+
# Arguments in argfile also work for repeatable and unique flags
23+
- long: --header
24+
short: -H
25+
arg: value
26+
repeatable: true
27+
unique: true
28+
help: Add an HTTP header

examples/argfile/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ bashly generate
88

99
### Try Me ###
1010

11+
./download --version
1112
./download somesource
1213
./download somesource --log cli.log
14+
./download somesource --header "x-from-cli: 2"
15+
16+
# demonstrating uniqueness across command line and argfile
17+
./download somesource --header "x-from-file: 1"

lib/bashly/script/introspection/commands.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ 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-
1510
# Returns a full list of the Command names and aliases combined
1611
def command_aliases
1712
commands.map(&:aliases).flatten
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
= view_marker
22

3-
> load_command_argfile "{{ argfile }}" "$@"
4-
> set -- "${argfile_input[@]}"
3+
> local argfile_line argfile_key argfile_value escaped
4+
> [[ -f "{{ argfile }}" ]] || return
5+
>
6+
> while IFS= read -r argfile_line || [[ -n "$argfile_line" ]]; do
7+
> [[ "$argfile_line" =~ ^[[:space:]]*(-{1,2}[^[:space:]]+)([[:space:]]+(.+))?[[:space:]]*$ ]] || continue
8+
> argfile_key="${BASH_REMATCH[1]}"
9+
> argfile_value="${BASH_REMATCH[3]:-}"
10+
> argfile_value="${argfile_value#"${argfile_value%%[![:space:]]*}"}"
11+
> argfile_value="${argfile_value%"${argfile_value##*[![:space:]]}"}"
12+
> [[ "$argfile_value" =~ ^\"(.*)\"$ || "$argfile_value" =~ ^\'(.*)\'$ ]] && argfile_value="${BASH_REMATCH[1]}"
13+
>
14+
> case "$argfile_key" in
15+
= flags.map { |flag| flag.render(:argfile_case) }.join.indent 4
16+
> esac
17+
> done <"{{ argfile }}"
518
>

lib/bashly/views/command/argfile_helpers.gtx

Lines changed: 0 additions & 36 deletions
This file was deleted.

lib/bashly/views/command/master_script.gtx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
= render :version_command
55
= render :usage
66
= render :normalize_input
7-
= render :argfile_helpers if argfile_used_anywhere?
87
= render :inspect_args if Settings.enabled? :inspect_args
98
= render :user_lib if user_lib.any?
109
= render :command_functions

lib/bashly/views/command/parse_requirements.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ end
88
> local key
99
>
1010

11-
= render(:argfile_filter).indent 2 if argfile
1211
= render(:fixed_flags_filter).indent 2
12+
= render(:argfile_filter).indent 2 if argfile
1313
= render(:environment_variables_filter).indent 2
1414
= render(:dependencies_filter).indent 2
1515
= render(:command_filter).indent 2

0 commit comments

Comments
 (0)