Skip to content

Commit 65d06d5

Browse files
authored
Merge pull request #182 from DannyBen/add/exclusive
Add support for exclusive flags (conflicts)
2 parents d890c09 + 88ae8b3 commit 65d06d5

File tree

20 files changed

+210
-2
lines changed

20 files changed

+210
-2
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

examples/filters/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ commands:
5454
# These filter functions can reside in any path under the `lib` directory.
5555
# You can use a single file for all filter functions, or a separate file
5656
# for each function.
57+
# Note that the `${args[]}` array is available to you in your filter functions.
5758

5859
# Print an error string if docker is not running.
5960
# The script will automatically exit if this function prints anything.

examples/filters/src/lib/filters.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# These filter functions can reside in any path under the `lib` directory.
22
# You can use a single file for all filter functions, or a separate file
33
# for each function.
4+
# Note that the `${args[]}` array is available to you in your filter functions.
45

56
# Print an error string if docker is not running.
67
# The script will automatically exit if this function prints anything.

examples/repeatable/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ inspect_args
7676
### `$ ./download -h`
7777

7878
```shell
79-
download - Sample application to demonstrate use of repeatable flags
79+
download - Sample application to demonstrate the use of repeatable flags
8080
8181
Usage:
8282
download [options]

0 commit comments

Comments
 (0)