Skip to content

Commit 736ced8

Browse files
committed
- Change command.argfile to apply defaults instead of prepending the command line
1 parent 2fca56b commit 736ced8

File tree

12 files changed

+74
-52
lines changed

12 files changed

+74
-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/README.md

Lines changed: 17 additions & 4 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:
@@ -49,6 +49,9 @@ flags:
4949

5050
````
5151

52+
Only flag lines are loaded from the argfile. Each flag value must appear on the
53+
same line as the flag. Non-flag lines are ignored.
54+
5255

5356
## Output
5457

@@ -67,6 +70,17 @@ args:
6770

6871
````
6972

73+
### `$ ./download --help`
74+
75+
````shell
76+
download - Sample application with autoloaded arguments
77+
78+
Usage:
79+
download SOURCE [OPTIONS]
80+
download --help | -h
81+
download --version | -v
82+
````
83+
7084
### `$ ./download somesource --log cli.log`
7185

7286
````shell
@@ -83,4 +97,3 @@ args:
8397
````
8498

8599

86-

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
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
= view_marker
2+
3+
> {{ aliases.join " | " }})
4+
= render(arg ? :argfile_case_arg : :argfile_case_no_arg).indent 2
5+
> ;;
6+
>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
= view_marker
2+
3+
> if [[ -n "$argfile_value" ]]; then
4+
5+
if repeatable
6+
> escaped="$(printf '%q' "$argfile_value")"
7+
> if [[ -z ${args['{{ name }}']+x} ]]; then
8+
> args['{{ name }}']="$escaped"
9+
if unique
10+
> unique_lookup["{{ name }}:$escaped"]=1
11+
> elif [[ -z "${unique_lookup["{{ name }}:$escaped"]:-}" ]]; then
12+
> args['{{ name }}']="${args['{{ name }}']} $escaped"
13+
> unique_lookup["{{ name }}:$escaped"]=1
14+
else
15+
> else
16+
> args['{{ name }}']="${args['{{ name }}']} $escaped"
17+
end
18+
> fi
19+
else
20+
> [[ -n ${args['{{ name }}']+x} ]] || args['{{ name }}']="$argfile_value"
21+
end
22+
23+
> fi
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
= view_marker
2+
3+
if repeatable
4+
> ((args['{{ name }}'] += 1))
5+
else
6+
> [[ -n ${args['{{ name }}']+x} ]] || args['{{ name }}']=1
7+
end

0 commit comments

Comments
 (0)