Skip to content

Commit f080ef2

Browse files
authored
Merge pull request #399 from DannyBen/add/commands_dir
Add suport for auto-organizing command files in subfolders
2 parents 1632c2a + 40ddcb1 commit f080ef2

File tree

18 files changed

+383
-13
lines changed

18 files changed

+383
-13
lines changed

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Each of these examples demonstrates one aspect or feature of bashly.
4646
- [custom-includes](custom-includes#readme) - adding and organizing your custom functions
4747
- [custom-script-header](custom-script-header#readme) - configuring a different script header
4848
- [footer](footer#readme) - adding a footer to the help message
49-
- [command-filenames](command-filenames#readme) - configuring paths for your source scripts
49+
- [command-filenames](command-filenames#readme) - overriding filenames for your source scripts
50+
- [command-paths](command-paths#readme) - configuring nested paths for your source scripts
5051
- [command-function](command-function#readme) - configuring custom internal function names
5152
- [split-config](split-config#readme) - splitting your `bashly.yml` into several smaller files
5253

examples/command-filenames/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Command Filenames Example
22

3-
Demonstrates how to specify custom filenames for command source files.
4-
This is useful for scripts with many commands, in case you wish to organize
5-
your source files in sub-folders.
3+
Demonstrates how to specify a custom filename for individual command source
4+
files.
65

7-
Note that the specified path is relative to the `sec` folder.
6+
Note that the specified path is relative to the `src` folder.
7+
8+
If you wish to have all your command files organized in sub-directories, use
9+
the [Command Paths Example](/examples/command-paths#readme) instead.
810

911
This example was generated with:
1012

examples/command-paths/.gitignore

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

examples/command-paths/README.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Command Paths Example
2+
3+
Demonstrates how to globally change the path used for command files and instruct
4+
bashly to read/write command files to nested sub-directories.
5+
6+
This is useful for scripts with many commands,
7+
8+
Note that the specified path is relative to the `src` folder.
9+
10+
See also: [Command Filenames Example](/examples/command-filenames#readme)
11+
12+
This example was generated with:
13+
14+
```bash
15+
$ bashly init
16+
$ bashly add settings
17+
# ... now edit src/settings.yml to match the example ...
18+
$ bashly generate
19+
```
20+
21+
<!-- include: settings.yml -->
22+
23+
-----
24+
25+
## `bashly.yml`
26+
27+
```yaml
28+
name: cli
29+
help: Sample application
30+
version: 0.1.0
31+
32+
environment_variables:
33+
- name: api_key
34+
help: Set your API key
35+
36+
commands:
37+
- name: download
38+
alias: d
39+
help: Download a file
40+
41+
args:
42+
- name: source
43+
required: true
44+
help: URL to download from
45+
- name: target
46+
help: "Target filename (default: same as source)"
47+
48+
flags:
49+
- long: --force
50+
short: -f
51+
help: Overwrite existing files
52+
53+
examples:
54+
- cli download example.com
55+
- cli download example.com ./output -f
56+
57+
environment_variables:
58+
- name: default_target_location
59+
help: Set the default location to download to
60+
61+
- name: upload
62+
alias: u
63+
help: Upload a file
64+
args:
65+
- name: source
66+
required: true
67+
help: File to upload
68+
69+
flags:
70+
- long: --user
71+
short: -u
72+
arg: user
73+
help: Username to use for logging in
74+
required: true
75+
- long: --password
76+
short: -p
77+
arg: password
78+
help: Password to use for logging in
79+
```
80+
81+
## `settings.yml`
82+
83+
```yaml
84+
# The path to use for command files, relative to source_dir
85+
# When set to nil (~), command files will be placed directly under source_dir
86+
# When set to any other string, command files will be placed under this
87+
# directory, and each command will get its own subdirectory
88+
89+
# commands_dir: ~
90+
commands_dir: commands
91+
92+
```
93+
94+
95+
## Generated script output
96+
97+
### `$ ./cli`
98+
99+
```shell
100+
cli - Sample application
101+
102+
Usage:
103+
cli COMMAND
104+
cli [COMMAND] --help | -h
105+
cli --version | -v
106+
107+
Commands:
108+
download Download a file
109+
upload Upload a file
110+
111+
112+
113+
```
114+
115+
### `$ ./cli -h`
116+
117+
```shell
118+
cli - Sample application
119+
120+
Usage:
121+
cli COMMAND
122+
cli [COMMAND] --help | -h
123+
cli --version | -v
124+
125+
Commands:
126+
download Download a file
127+
upload Upload a file
128+
129+
Options:
130+
--help, -h
131+
Show this help
132+
133+
--version, -v
134+
Show version number
135+
136+
Environment Variables:
137+
API_KEY
138+
Set your API key
139+
140+
141+
142+
```
143+
144+
### `$ ./cli download something`
145+
146+
```shell
147+
# this file is located in 'src/commands/download.sh'
148+
# code for 'cli download' goes here
149+
# you can edit it freely and regenerate (it will not be overwritten)
150+
args:
151+
- ${args[source]} = something
152+
153+
154+
```
155+
156+
### `$ ls -1 src/*`
157+
158+
```shell
159+
src/bashly.yml
160+
161+
src/commands:
162+
download.sh
163+
upload.sh
164+
165+
166+
```
167+
168+
169+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The path to use for command files, relative to source_dir
2+
# When set to nil (~), command files will be placed directly under source_dir
3+
# When set to any other string, command files will be placed under this
4+
# directory, and each command will get its own subdirectory
5+
6+
# commands_dir: ~
7+
commands_dir: commands
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: cli
2+
help: Sample application
3+
version: 0.1.0
4+
5+
environment_variables:
6+
- name: api_key
7+
help: Set your API key
8+
9+
commands:
10+
- name: download
11+
alias: d
12+
help: Download a file
13+
14+
args:
15+
- name: source
16+
required: true
17+
help: URL to download from
18+
- name: target
19+
help: "Target filename (default: same as source)"
20+
21+
flags:
22+
- long: --force
23+
short: -f
24+
help: Overwrite existing files
25+
26+
examples:
27+
- cli download example.com
28+
- cli download example.com ./output -f
29+
30+
environment_variables:
31+
- name: default_target_location
32+
help: Set the default location to download to
33+
34+
- name: upload
35+
alias: u
36+
help: Upload a file
37+
args:
38+
- name: source
39+
required: true
40+
help: File to upload
41+
42+
flags:
43+
- long: --user
44+
short: -u
45+
arg: user
46+
help: Username to use for logging in
47+
required: true
48+
- long: --password
49+
short: -p
50+
arg: password
51+
help: Password to use for logging in
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/commands/download.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/commands/upload.sh'"
2+
echo "# code for 'cli upload' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args

examples/command-paths/test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
rm -rf ./src/commands
4+
5+
set -x
6+
7+
bashly generate
8+
9+
### Try Me ###
10+
11+
./cli
12+
./cli -h
13+
./cli download something
14+
ls -1 src/*

lib/bashly/extensions/string.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def to_underscore
1313
gsub(/(.)([A-Z])/, '\1_\2').gsub(/[- ]/, '_').downcase
1414
end
1515

16+
def to_path
17+
tr(' ', '/').downcase
18+
end
19+
1620
def wrap(length = 80)
1721
strip!
1822
split("\n").collect! do |line|

0 commit comments

Comments
 (0)