Skip to content

Commit ef95fbe

Browse files
committed
- Add support for exclusive flags (conflicts)
1 parent 1e09ed5 commit ef95fbe

File tree

15 files changed

+196
-0
lines changed

15 files changed

+196
-0
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
2525
- [extensible-delegate](extensible-delegate#readme) - extending your script by delegating commands to an external executable
2626
- [whitelist](whitelist#readme) - arguments and flags with a predefined allowed list of values
2727
- [repeatable](repeatable#readme) - allowing flags to be provided multiple times
28+
- [conflicts](conflicts#readme) - defining mutually exclusive flags
2829
- [command-private](command-private#readme) - hiding commands from the command list
2930
- [stdin](stdin#readme) - reading input from stdin
3031
- [filters](filters#readme) - preventing commands from running unless custom conditions are met

examples/conflicts/.gitignore

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

examples/conflicts/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Conflicting Flags Example
2+
3+
Demonstrates the use of conflicting flags that cannot be executed together.
4+
5+
This example was generated with:
6+
7+
```bash
8+
$ bashly init --minimal
9+
# ... now edit src/bashly.yml to match the example ...
10+
$ bashly generate
11+
```
12+
13+
-----
14+
15+
## `bashly.yml`
16+
17+
```yaml
18+
name: download
19+
help: Sample application to demonstrate the use of conflicting flags
20+
version: 0.1.0
21+
22+
flags:
23+
- long: --cache
24+
help: Enable cache
25+
# Running --cache with --no-cache is not permitted
26+
conflicts: [--no-cache]
27+
- long: --no-cache
28+
help: Diisable cache
29+
# Running --no-cache with --cache or with --fast is not permitted
30+
conflicts: [--cache, --fast]
31+
- long: --fast
32+
help: Run faster
33+
# Make sure to add the conflicting flags in both flags
34+
conflicts: [--no-cache]
35+
```
36+
37+
38+
39+
## Generated script output
40+
41+
### `$ ./download -h`
42+
43+
```shell
44+
download - Sample application to demonstrate the use of conflicting flags
45+
46+
Usage:
47+
download [options]
48+
download --help | -h
49+
download --version | -v
50+
51+
Options:
52+
--help, -h
53+
Show this help
54+
55+
--version, -v
56+
Show version number
57+
58+
--cache
59+
Enable cache
60+
61+
--no-cache
62+
Diisable cache
63+
64+
--fast
65+
Run faster
66+
67+
68+
69+
```
70+
71+
### `$ ./download --cache`
72+
73+
```shell
74+
# this file is located in 'src/root_command.sh'
75+
# you can edit it freely and regenerate (it will not be overwritten)
76+
args:
77+
- ${args[--cache]} = 1
78+
79+
80+
```
81+
82+
### `$ ./download --no-cache --fast`
83+
84+
```shell
85+
conflicting options: --fast cannot be used with --no-cache
86+
87+
88+
```
89+
90+
91+

examples/conflicts/src/bashly.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: download
2+
help: Sample application to demonstrate the use of conflicting flags
3+
version: 0.1.0
4+
5+
flags:
6+
- long: --cache
7+
help: Enable cache
8+
# Running --cache with --no-cache is not permitted
9+
conflicts: [--no-cache]
10+
- long: --no-cache
11+
help: Diisable cache
12+
# Running --no-cache with --cache or with --fast is not permitted
13+
conflicts: [--cache, --fast]
14+
- long: --fast
15+
help: Run faster
16+
# Make sure to add the conflicting flags in both flags
17+
conflicts: [--no-cache]
18+

examples/conflicts/src/initialize.sh

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

examples/conflicts/test.sh

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

lib/bashly/config_validator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def assert_flag(key, value)
9999

100100
assert_boolean "#{key}.required", value['required']
101101
assert_array "#{key}.allowed", value['allowed'], of: :string
102+
assert_array "#{key}.conflicts", value['conflicts'], of: :string
102103

103104
assert value['long'].match(/^--[a-zA-Z0-9_\-]+$/), "#{key}.long must be in the form of '--name'" if value['long']
104105
assert value['short'].match(/^-[a-zA-Z0-9]$/), "#{key}.short must be in the form of '-n'" if value['short']

lib/bashly/script/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Base
1010
arg
1111
catch_all
1212
completions
13+
conflicts
1314
default
1415
dependencies
1516
description

lib/bashly/templates/strings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ version_flag_text: Show version number
2626
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
2727
invalid_argument: "invalid argument: %s"
2828
invalid_flag: "invalid option: %s"
29+
conflicting_flags: "conflicting options: %s cannot be used with %s"
2930
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
3031
missing_required_flag: "missing required flag: %{usage}"
3132
missing_required_environment_variable: "missing required environment variable: %{var}"

0 commit comments

Comments
 (0)