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 @@ -25,6 +25,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
- [repeatable](repeatable#readme) - allowing flags to be provided multiple times
- [conflicts](conflicts#readme) - defining mutually exclusive flags
- [command-private](command-private#readme) - hiding commands from the command list
- [stdin](stdin#readme) - reading input from stdin
- [filters](filters#readme) - preventing commands from running unless custom conditions are met
Expand Down
1 change: 1 addition & 0 deletions examples/conflicts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
download
91 changes: 91 additions & 0 deletions examples/conflicts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Conflicting Flags Example

Demonstrates the use of conflicting flags that cannot be executed together.

This example was generated with:

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

-----

## `bashly.yml`

```yaml
name: download
help: Sample application to demonstrate the use of conflicting flags
version: 0.1.0

flags:
- long: --cache
help: Enable cache
# Running --cache with --no-cache is not permitted
conflicts: [--no-cache]
- long: --no-cache
help: Diisable cache
# Running --no-cache with --cache or with --fast is not permitted
conflicts: [--cache, --fast]
- long: --fast
help: Run faster
# Make sure to add the conflicting flags in both flags
conflicts: [--no-cache]
```



## Generated script output

### `$ ./download -h`

```shell
download - Sample application to demonstrate the use of conflicting flags

Usage:
download [options]
download --help | -h
download --version | -v

Options:
--help, -h
Show this help

--version, -v
Show version number

--cache
Enable cache

--no-cache
Diisable cache

--fast
Run faster



```

### `$ ./download --cache`

```shell
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--cache]} = 1


```

### `$ ./download --no-cache --fast`

```shell
conflicting options: --fast cannot be used with --no-cache


```



18 changes: 18 additions & 0 deletions examples/conflicts/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: download
help: Sample application to demonstrate the use of conflicting flags
version: 0.1.0

flags:
- long: --cache
help: Enable cache
# Running --cache with --no-cache is not permitted
conflicts: [--no-cache]
- long: --no-cache
help: Diisable cache
# Running --no-cache with --cache or with --fast is not permitted
conflicts: [--cache, --fast]
- long: --fast
help: Run faster
# Make sure to add the conflicting flags in both flags
conflicts: [--no-cache]

6 changes: 6 additions & 0 deletions examples/conflicts/src/initialize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Code here runs inside the initialize() function
## Use it for anything that you need to run before any other function, like
## setting environment vairables:
## CONFIG_FILE=settings.ini
##
## Feel free to empty (but not delete) this file.
3 changes: 3 additions & 0 deletions examples/conflicts/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "# this file is located in 'src/root_command.sh'"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
11 changes: 11 additions & 0 deletions examples/conflicts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -x

bashly generate

### Try Me ###

./download -h
./download --cache
./download --no-cache --fast
1 change: 1 addition & 0 deletions examples/filters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ commands:
# These filter functions can reside in any path under the `lib` directory.
# You can use a single file for all filter functions, or a separate file
# for each function.
# Note that the `${args[]}` array is available to you in your filter functions.

# Print an error string if docker is not running.
# The script will automatically exit if this function prints anything.
Expand Down
1 change: 1 addition & 0 deletions examples/filters/src/lib/filters.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# These filter functions can reside in any path under the `lib` directory.
# You can use a single file for all filter functions, or a separate file
# for each function.
# Note that the `${args[]}` array is available to you in your filter functions.

# Print an error string if docker is not running.
# The script will automatically exit if this function prints anything.
Expand Down
2 changes: 1 addition & 1 deletion examples/repeatable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ inspect_args
### `$ ./download -h`

```shell
download - Sample application to demonstrate use of repeatable flags
download - Sample application to demonstrate the use of repeatable flags

Usage:
download [options]
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def assert_flag(key, value)

assert_boolean "#{key}.required", value['required']
assert_array "#{key}.allowed", value['allowed'], of: :string
assert_array "#{key}.conflicts", value['conflicts'], of: :string

assert value['long'].match(/^--[a-zA-Z0-9_\-]+$/), "#{key}.long must be in the form of '--name'" if value['long']
assert value['short'].match(/^-[a-zA-Z0-9]$/), "#{key}.short must be in the form of '-n'" if value['short']
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/script/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Base
arg
catch_all
completions
conflicts
default
dependencies
description
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/templates/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ version_flag_text: Show version number
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
invalid_argument: "invalid argument: %s"
invalid_flag: "invalid option: %s"
conflicting_flags: "conflicting options: %s cannot be used with %s"
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
missing_required_flag: "missing required flag: %{usage}"
missing_required_environment_variable: "missing required environment variable: %{var}"
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/views/command/parse_requirements.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ parse_requirements() {
<%= render(:fixed_flags_filter).indent 2 %>
<%= render(:environment_variables_filter).indent 2 %>
<%= render(:dependencies_filter).indent 2 %>
<%= render(:user_filter).indent 2 %>
<%= render(:command_filter).indent 2 %>
<%= render(:parse_requirements_while).indent 2 %>
<%= render(:required_args_filter).indent 2 %>
<%= render(:required_flags_filter).indent 2 %>
<%= render(:catch_all_filter).indent 2 %>
<%= render(:default_assignments).indent 2 %>
<%= render(:whitelist_filter).indent 2 %>
<%= render(:user_filter).indent 2 %>
}

% commands.each do |command|
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/views/flag/case.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# :flag.case
<%= aliases.join " | " %> )
<%= render(:conflicts).indent 2 %>
% if arg
if [[ -n ${2+x} ]]; then
<%= render(:validations).indent 4 %>
Expand Down
18 changes: 18 additions & 0 deletions lib/bashly/views/flag/conflicts.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# :flag.conflicts
% if conflicts
% if conflicts.count == 1
if [[ -n "${args[<%= conflicts.first %>]:-}" ]]; then
printf "<%= strings[:conflicting_flags] %>\n" "$key" "<%= conflicts.first %>"
exit 1
fi

% else
for conflict in <%= conflicts.join ' ' %>; do
if [[ -n "${args[$conflict]:-}" ]]; then
printf "<%= strings[:conflicting_flags] %>\n" "$key" "$conflict"
exit 1
fi
done

% end
% end
37 changes: 37 additions & 0 deletions spec/approvals/examples/conflicts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
+ bashly generate
creating user files in src
skipped src/initialize.sh (exists)
skipped src/root_command.sh (exists)
created ./download
run ./download --help to test your bash script
+ ./download -h
download - Sample application to demonstrate the use of conflicting flags

Usage:
download [options]
download --help | -h
download --version | -v

Options:
--help, -h
Show this help

--version, -v
Show version number

--cache
Enable cache

--no-cache
Diisable cache

--fast
Run faster

+ ./download --cache
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--cache]} = 1
+ ./download --no-cache --fast
conflicting options: --fast cannot be used with --no-cache
1 change: 1 addition & 0 deletions spec/approvals/validations/invalid_conflicts_array
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.flags[0].conflicts[0] must be a string>
1 change: 1 addition & 0 deletions spec/approvals/validations/invalid_conflicts_type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.flags[0].conflicts must be an array>
14 changes: 14 additions & 0 deletions spec/fixtures/script/validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,17 @@
help: invalid since command.filters should be an array of strings
filters: [1, 2]

:invalid_conflicts_type:
name: invalid
help: invalid since flag.conflicts should be an array
flags:
- long: --cache
conflicts: --no-cache

:invalid_conflicts_array:
name: invalid
help: invalid since flag.conflicts should be an array of strings
flags:
- long: --cache
conflicts: [1, 2]