bashly 0.3.6 → 0.4.1rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +85 -16
- data/lib/bashly/models/command.rb +11 -1
- data/lib/bashly/models/flag.rb +4 -0
- data/lib/bashly/templates/lib/config.sh +15 -4
- data/lib/bashly/templates/lib/sample_function.sh +1 -1
- data/lib/bashly/templates/strings.yml +2 -1
- data/lib/bashly/version.rb +1 -1
- data/lib/bashly/views/argument/usage.erb +3 -0
- data/lib/bashly/views/command/default_assignments.erb +7 -0
- data/lib/bashly/views/command/inspect_args.erb +7 -2
- data/lib/bashly/views/command/parse_requirements.erb +1 -0
- data/lib/bashly/views/flag/case.erb +4 -4
- data/lib/bashly/views/flag/usage.erb +3 -0
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e31810f90eb5199f74dd9fd0dc0aedfb4bfd5dd4f38bc4e36473ab6c3e4f10d2
|
4
|
+
data.tar.gz: 5de458e2eea4ec796e44a67cddeabaf848ef069ec2d37ad7ddb1f3cc660bad27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72a03554e17b648efda96ac0208bd4ee8223680717933a71907994ca3a3db3ffd629ea3572fc27143babb6634a65ad2f0b83fdeaeea2ab01d0c4755ea630f674
|
7
|
+
data.tar.gz: eb955f344c428e92b82150b95256f3c9d90e3461353bb6b13cad68eeb17bf5e6220991590aa1d797b447ee5c92f414ef9b5042b13569a284595dac0cadd7babd
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
<div align='center'>
|
2
2
|
<img src='logo.svg' width=280>
|
3
3
|
|
4
|
-
Bashly - Bash CLI Framework and Generator
|
5
|
-
==================================================
|
4
|
+
# Bashly - Bash CLI Framework and Generator
|
6
5
|
|
7
6
|
Create beautiful bash scripts from simple YAML configuration
|
8
7
|
|
@@ -16,18 +15,46 @@ Create beautiful bash scripts from simple YAML configuration
|
|
16
15
|
|
17
16
|
</div>
|
18
17
|
|
18
|
+
## Table of Contents
|
19
|
+
|
20
|
+
- [Table of Contents](#table-of-contents)
|
21
|
+
- [Installation](#installation)
|
22
|
+
- [Prerequisites](#prerequisites)
|
23
|
+
- [What is Bashly](#what-is-bashly)
|
24
|
+
- [Usage](#usage)
|
25
|
+
- [Using the input arguemnts in your code](#using-the-input-arguemnts-in-your-code)
|
26
|
+
- [Examples](#examples)
|
27
|
+
- [Sample configuraiton for a script without commands](#sample-configuraiton-for-a-script-without-commands)
|
28
|
+
- [Sample configuraiton for a script with commands](#sample-configuraiton-for-a-script-with-commands)
|
29
|
+
- [Configuration Reference](#configuration-reference)
|
30
|
+
- [Command options](#command-options)
|
31
|
+
- [Argument options](#argument-options)
|
32
|
+
- [Flag options](#flag-options)
|
33
|
+
- [Environment Variable options](#environment-variable-options)
|
34
|
+
- [Real World Examples](#real-world-examples)
|
35
|
+
- [Contributing / Support](#contributing--support)
|
36
|
+
|
19
37
|
---
|
20
38
|
|
21
|
-
Installation
|
22
|
-
--------------------------------------------------
|
39
|
+
## Installation
|
23
40
|
|
24
41
|
```shell
|
25
42
|
$ gem install bashly
|
26
43
|
```
|
27
44
|
|
45
|
+
or with Docker:
|
46
|
+
|
47
|
+
```shell
|
48
|
+
$ alias bashly='docker run --rm -it --volume "$PWD:/app" dannyben/bashly'
|
49
|
+
```
|
50
|
+
|
51
|
+
## Prerequisites
|
52
|
+
|
53
|
+
The bash scripts generated by bashly require bash 4 or higher due to heavy
|
54
|
+
use of associative arrays.
|
28
55
|
|
29
|
-
|
30
|
-
|
56
|
+
|
57
|
+
## What is Bashly
|
31
58
|
|
32
59
|
Bashly is a command line application (written in Ruby) that lets you generate
|
33
60
|
feature-rich bash command line tools.
|
@@ -54,9 +81,9 @@ Bahsly is responsible for:
|
|
54
81
|
- **Color output**.
|
55
82
|
- **Config file management** (INI format).
|
56
83
|
- **YAML parsing**.
|
84
|
+
- and more.
|
57
85
|
|
58
|
-
Usage
|
59
|
-
--------------------------------------------------
|
86
|
+
## Usage
|
60
87
|
|
61
88
|
In an empty directory, create a sample configuration file by running
|
62
89
|
|
@@ -86,9 +113,46 @@ Finally, edit the files in the `src` folder. Each of your script's commands
|
|
86
113
|
get their own file. Once you edit, run `bashly generate` again to merge the
|
87
114
|
content from your functions back into the script.
|
88
115
|
|
116
|
+
### Using the input arguemnts in your code
|
117
|
+
|
118
|
+
In order to access the parsed arguments in any of your partial scripts, you
|
119
|
+
may simply access the `$args` associative array.
|
120
|
+
|
121
|
+
For example:
|
122
|
+
|
123
|
+
1. Generate a minimal configuration with `bashly init --minimal`
|
124
|
+
2. Generate the bash script with `bashly generate`
|
125
|
+
3. Run the script with `./download hello --force`
|
126
|
+
|
127
|
+
You will notice that all the arguments of the associative array are printed
|
128
|
+
on screen. This is done by the `inspect_args` function that was inserted into
|
129
|
+
the generated partial script `src/root_command.sh`.
|
89
130
|
|
90
|
-
|
91
|
-
|
131
|
+
You can now access these variables by modifying `sec/root_command.sh` like
|
132
|
+
this:
|
133
|
+
|
134
|
+
|
135
|
+
```bash
|
136
|
+
# src/root_command.sh
|
137
|
+
source_url=${args[source]}
|
138
|
+
force=${args[--force]}
|
139
|
+
|
140
|
+
if [[ $force ]]; then
|
141
|
+
echo "downloading $source_url with --force"
|
142
|
+
else
|
143
|
+
echo "downloading $source_url"
|
144
|
+
fi
|
145
|
+
```
|
146
|
+
|
147
|
+
After editing the file, run `bashly generate` (or `bashly g` for short) and
|
148
|
+
run:
|
149
|
+
|
150
|
+
```
|
151
|
+
$ ./download a --force
|
152
|
+
downloading a with --force
|
153
|
+
```
|
154
|
+
|
155
|
+
## Examples
|
92
156
|
|
93
157
|
The `bashly.yml` file can be set up to generate two types of scripts:
|
94
158
|
|
@@ -116,8 +180,7 @@ See the [examples](examples) folder for more examples.
|
|
116
180
|
|
117
181
|
|
118
182
|
|
119
|
-
Configuration Reference
|
120
|
-
--------------------------------------------------
|
183
|
+
## Configuration Reference
|
121
184
|
|
122
185
|
The `bashly.yml` configuration file consists of these types:
|
123
186
|
|
@@ -159,6 +222,7 @@ bash function.
|
|
159
222
|
`name` | The name of the argument.
|
160
223
|
`help` | The message to display when using `--help`. Can have multiple lines.
|
161
224
|
`required` | Specify if this argument is required. Note that once you define an optional argument (without required: true) then you cannot define required arguments after it.
|
225
|
+
`default` | The value to use in case it is not provided by the user. Implies that this argument is optional.
|
162
226
|
|
163
227
|
### Flag options
|
164
228
|
|
@@ -173,6 +237,7 @@ short form).
|
|
173
237
|
`help` | The text to display when using `--help`. Can have multiple lines.
|
174
238
|
`arg` | If the flag requires an argument, specify its name here.
|
175
239
|
`required` | Specify if this flag is required.
|
240
|
+
`default` | The value to use in case it is not provided by the user. Implies that this flag is optional, and only makes sense when the flag has an argument.
|
176
241
|
|
177
242
|
#### Special handling for -v and -h
|
178
243
|
|
@@ -195,17 +260,21 @@ set.
|
|
195
260
|
`required` | Specify if this variable is required.
|
196
261
|
|
197
262
|
|
198
|
-
Real World Examples
|
199
|
-
--------------------------------------------------
|
263
|
+
## Real World Examples
|
200
264
|
|
201
265
|
- [Rush][rush] - a Personal Package Manager
|
266
|
+
- [Alf][alf] - a generator for bash aliases and sub-aliases
|
267
|
+
- [git-changelog][git-changelog] - a change log generator
|
202
268
|
|
203
269
|
|
204
|
-
Contributing / Support
|
205
|
-
--------------------------------------------------
|
270
|
+
## Contributing / Support
|
206
271
|
|
207
272
|
If you experience any issue, have a question or a suggestion, or if you wish
|
208
273
|
to contribute, feel free to [open an issue][issues].
|
209
274
|
|
210
275
|
[issues]: https://github.com/DannyBen/bashly/issues
|
211
276
|
[rush]: https://github.com/DannyBen/rush-cli
|
277
|
+
[alf]: https://github.com/DannyBen/alf
|
278
|
+
[git-changelog]: https://github.com/DannyBen/git-changelog
|
279
|
+
|
280
|
+
|
@@ -55,12 +55,22 @@ module Bashly
|
|
55
55
|
result
|
56
56
|
end
|
57
57
|
|
58
|
+
# Returns an array of all the default Args
|
59
|
+
def default_args
|
60
|
+
args.select &:default
|
61
|
+
end
|
62
|
+
|
58
63
|
# If any of this command's subcommands has the default option set to
|
59
64
|
# true, this default command will be returned, nil otherwise.
|
60
65
|
def default_command
|
61
66
|
commands.find { |c| c.default }
|
62
67
|
end
|
63
68
|
|
69
|
+
# Returns an array of all the default Flags
|
70
|
+
def default_flags
|
71
|
+
flags.select &:default
|
72
|
+
end
|
73
|
+
|
64
74
|
# Returns an array of EnvironmentVariables
|
65
75
|
def environment_variables
|
66
76
|
return [] unless options["environment_variables"]
|
@@ -148,7 +158,7 @@ module Bashly
|
|
148
158
|
args.each do |arg|
|
149
159
|
result << arg.usage_string
|
150
160
|
end
|
151
|
-
result << "[options]"
|
161
|
+
result << "[options]" unless flags.empty?
|
152
162
|
result.join " "
|
153
163
|
end
|
154
164
|
|
data/lib/bashly/models/flag.rb
CHANGED
@@ -21,7 +21,7 @@ config_init() {
|
|
21
21
|
# Usage: result=$(config_get hello)
|
22
22
|
config_get() {
|
23
23
|
key=$1
|
24
|
-
regex="^$key
|
24
|
+
regex="^$key *= *(.+)$"
|
25
25
|
|
26
26
|
config_init
|
27
27
|
|
@@ -44,7 +44,7 @@ config_set() {
|
|
44
44
|
|
45
45
|
config_init
|
46
46
|
|
47
|
-
regex="^($key)
|
47
|
+
regex="^($key) *= *.+$"
|
48
48
|
output=""
|
49
49
|
found_key=""
|
50
50
|
|
@@ -71,7 +71,7 @@ config_set() {
|
|
71
71
|
config_del() {
|
72
72
|
key=$1
|
73
73
|
|
74
|
-
regex="^($key)
|
74
|
+
regex="^($key) *="
|
75
75
|
output=""
|
76
76
|
|
77
77
|
config_init
|
@@ -100,7 +100,7 @@ config_show() {
|
|
100
100
|
# done
|
101
101
|
#
|
102
102
|
config_keys() {
|
103
|
-
regex="^(
|
103
|
+
regex="^([a-zA-Z0-9_\-\/\.]+) *="
|
104
104
|
|
105
105
|
config_init
|
106
106
|
|
@@ -113,3 +113,14 @@ config_keys() {
|
|
113
113
|
done < "$CONFIG_FILE"
|
114
114
|
echo "${keys[@]}"
|
115
115
|
}
|
116
|
+
|
117
|
+
# Returns true if the specified key exists in the config file
|
118
|
+
# Usage:
|
119
|
+
#
|
120
|
+
# if config_has_key "key" ; then
|
121
|
+
# echo "key exists"
|
122
|
+
# fi
|
123
|
+
#
|
124
|
+
config_has_key() {
|
125
|
+
[[ $(config_get "$1") ]]
|
126
|
+
}
|
@@ -14,13 +14,14 @@ group: "%{group} Commands:"
|
|
14
14
|
command_shortcut: "Shortcut: %{short}"
|
15
15
|
default_command_summary: "%{summary} (default)"
|
16
16
|
required: "(required)"
|
17
|
+
default: "Default: %{value}"
|
17
18
|
|
18
19
|
# Fixed flags help text
|
19
20
|
help_flag_text: Show this help
|
20
21
|
version_flag_text: Show version number
|
21
22
|
|
22
23
|
# Error messages
|
23
|
-
flag_requires_an_argument: "%{
|
24
|
+
flag_requires_an_argument: "%{name} requires an argument: %{usage}"
|
24
25
|
invalid_argument: "invalid argument: %s"
|
25
26
|
invalid_flag: "invalid option: %s"
|
26
27
|
missing_required_argument: "missing required argument: %{arg}\\nusage: %{usage}"
|
data/lib/bashly/version.rb
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
# :command.default_assignments
|
2
|
+
<%- default_args.each do |arg| -%>
|
3
|
+
[[ -n ${args[<%= arg.name %>]} ]] || args[<%= arg.name %>]="<%= arg.default %>"
|
4
|
+
<%- end -%>
|
5
|
+
<%- default_flags.each do |flag| -%>
|
6
|
+
[[ -n ${args[<%= flag.long %>]} ]] || args[<%= flag.long %>]="<%= flag.default %>"
|
7
|
+
<%- end -%>
|
@@ -1,5 +1,10 @@
|
|
1
1
|
# :command.inspect_args
|
2
2
|
inspect_args() {
|
3
|
-
|
4
|
-
|
3
|
+
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
|
4
|
+
if (( ${#args[@]} )); then
|
5
|
+
echo args:
|
6
|
+
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
|
7
|
+
else
|
8
|
+
echo args: none
|
9
|
+
fi
|
5
10
|
}
|
@@ -11,6 +11,7 @@ parse_requirements() {
|
|
11
11
|
<%= render(:required_args_filter).indent 2 %>
|
12
12
|
<%= render(:required_flags_filter).indent 2 %>
|
13
13
|
<%= render(:parse_requirements_while).indent 2 %>
|
14
|
+
<%= render(:default_assignments).indent 2 %>
|
14
15
|
}
|
15
16
|
|
16
17
|
<%- commands.each do |command| %>
|
@@ -1,16 +1,16 @@
|
|
1
1
|
# :flag.case
|
2
2
|
<%= aliases.join " | " %> )
|
3
3
|
<%- if arg -%>
|
4
|
-
if [[ $2
|
5
|
-
args[<%=
|
4
|
+
if [[ $2 ]]; then
|
5
|
+
args[<%= name %>]="$2"
|
6
6
|
shift
|
7
7
|
shift
|
8
8
|
else
|
9
|
-
printf "%s\n" "<%= strings[:flag_requires_an_argument] % {
|
9
|
+
printf "%s\n" "<%= strings[:flag_requires_an_argument] % { name: name, usage: usage_string } %>"
|
10
10
|
exit 1
|
11
11
|
fi
|
12
12
|
<%- else -%>
|
13
|
-
args[<%=
|
13
|
+
args[<%= name %>]=1
|
14
14
|
shift
|
15
15
|
<%- end -%>
|
16
16
|
;;
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bashly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colsole
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- lib/bashly/views/argument/usage.erb
|
94
94
|
- lib/bashly/views/command/command_filter.erb
|
95
95
|
- lib/bashly/views/command/command_functions.erb
|
96
|
+
- lib/bashly/views/command/default_assignments.erb
|
96
97
|
- lib/bashly/views/command/default_initialize_script.erb
|
97
98
|
- lib/bashly/views/command/default_root_script.erb
|
98
99
|
- lib/bashly/views/command/default_script.erb
|
@@ -137,11 +138,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
138
|
version: 2.3.0
|
138
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
140
|
requirements:
|
140
|
-
- - "
|
141
|
+
- - ">"
|
141
142
|
- !ruby/object:Gem::Version
|
142
|
-
version:
|
143
|
+
version: 1.3.1
|
143
144
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.2.3
|
145
146
|
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: Bash Command Line Tool Generator
|