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 @@ -38,6 +38,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
- [filters](filters#readme) - preventing commands from running unless custom conditions are met
- [commands-expose](commands-expose#readme) - showing sub-commands in the parent's help
- [key-value-pairs](key-value-pairs#readme) - parsing key=value arguments and flags
- [command-examples-on-error](command-examples-on-error#readme) - showing examples on error

## Customization

Expand Down
1 change: 1 addition & 0 deletions examples/command-examples-on-error/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
119 changes: 119 additions & 0 deletions examples/command-examples-on-error/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Command Examples on Error Example

Demonstrates how to show examples whenever the user does not provide all the
required arguments.

This example was generated with:

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

<!-- include: settings.yml -->

-----

## `bashly.yml`

````yaml
name: cli
help: Sample application
version: 0.1.0

commands:
- name: download
alias: d
help: Download a file

args:
- name: source
required: true
help: URL to download from
- name: target
help: "Target filename (default: same as source)"

flags:
- long: --force
short: -f
help: Overwrite existing files

# Examples can be provided either as an array, or as a string.
# The array form is convenient when you just want to provide one-liner
# examples.
examples:
- cli download example.com
- cli download example.com ./output -f

- name: upload
alias: u
help: Upload a file
args:
- name: source
required: true
help: File to upload

flags:
- long: --user
short: -u
arg: user
help: Username to use for logging in
required: true
- long: --password
short: -p
arg: password
help: Password to use for logging in

# The string form examples is useful when you wish to have more control
# over how the examples are displayed. Note the use of the '|-' marker
# that tells YAML to use the string as is, including the newlines it contains.
examples: |-
Upload a file
$ cli upload profile.png -u admin -p s3cr3t

Upload a file (you will be prompted to provide a password)
$ cli upload profile.png --user admin
````

## `settings.yml`

````yaml
show_examples_on_error: true

````


## Output

### `$ ./cli download`

````shell
missing required argument: SOURCE
usage: cli download SOURCE [TARGET] [OPTIONS]
examples:
cli download example.com
cli download example.com ./output -f


````

### `$ ./cli upload`

````shell
missing required argument: SOURCE
usage: cli upload SOURCE [OPTIONS]
examples:
Upload a file
$ cli upload profile.png -u admin -p s3cr3t

Upload a file (you will be prompted to provide a password)
$ cli upload profile.png --user admin


````



1 change: 1 addition & 0 deletions examples/command-examples-on-error/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
show_examples_on_error: true
58 changes: 58 additions & 0 deletions examples/command-examples-on-error/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: cli
help: Sample application
version: 0.1.0

commands:
- name: download
alias: d
help: Download a file

args:
- name: source
required: true
help: URL to download from
- name: target
help: "Target filename (default: same as source)"

flags:
- long: --force
short: -f
help: Overwrite existing files

# Examples can be provided either as an array, or as a string.
# The array form is convenient when you just want to provide one-liner
# examples.
examples:
- cli download example.com
- cli download example.com ./output -f

- name: upload
alias: u
help: Upload a file
args:
- name: source
required: true
help: File to upload

flags:
- long: --user
short: -u
arg: user
help: Username to use for logging in
required: true
- long: --password
short: -p
arg: password
help: Password to use for logging in

# The string form examples is useful when you wish to have more control
# over how the examples are displayed. Note the use of the '|-' marker
# that tells YAML to use the string as is, including the newlines it contains.
examples: |-
Upload a file
$ cli upload profile.png -u admin -p s3cr3t

Upload a file (you will be prompted to provide a password)
$ cli upload profile.png --user admin


4 changes: 4 additions & 0 deletions examples/command-examples-on-error/src/download_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "# this file is located in 'src/download_command.sh'"
echo "# code for 'cli download' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
4 changes: 4 additions & 0 deletions examples/command-examples-on-error/src/upload_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "# this file is located in 'src/upload_command.sh'"
echo "# code for 'cli upload' goes here"
echo "# you can edit it freely and regenerate (it will not be overwritten)"
inspect_args
12 changes: 12 additions & 0 deletions examples/command-examples-on-error/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

rm -f ./src/*.sh

set -x

bashly generate

### Try Me ###

./cli download
./cli upload
4 changes: 4 additions & 0 deletions lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ env: development
# The extension to use when reading/writing partial script snippets
partials_extension: sh

# Show command examples (if any) whenever the user does not provide the
# required arguments
show_examples_on_error: false

# Display various usage elements in color by providing the name of the color
# function. The value for each property is a name of a function that is
# available in your script, for example: `green` or `bold`.
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/libraries/strings/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ 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}"
missing_dependency: "missing dependency: %{dependency}"
examples_caption_on_error: 'examples:'
disallowed_flag: "%{name} must be one of: %{allowed}"
disallowed_argument: "%{name} must be one of: %{allowed}"
disallowed_environment_variable: "%{name} environment variable must be one of: %{allowed}"
Expand Down
5 changes: 5 additions & 0 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class << self
:config_path,
:lib_dir,
:partials_extension,
:show_examples_on_error,
:source_dir,
:strict,
:tab_indent,
Expand Down Expand Up @@ -57,6 +58,10 @@ def production?
env == :production
end

def show_examples_on_error
@show_examples_on_error ||= get :show_examples_on_error
end

def source_dir
@source_dir ||= get :source_dir
end
Expand Down
8 changes: 8 additions & 0 deletions lib/bashly/views/command/examples_on_error.gtx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if Settings.show_examples_on_error && examples.any?
= view_marker

> printf "{{ strings[:examples_caption_on_error] }}\n" >&2
examples.each do |example|
> printf "{{ example.wrap(78).indent(2).sanitize_for_print }}\n" >&2
end
end
4 changes: 2 additions & 2 deletions lib/bashly/views/command/required_args_filter.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ if required_args.any?
required_args.each do |arg|
> if [[ -z ${args['{{ arg.name }}']+x} ]]; then
> printf "{{ strings[:missing_required_argument] % { arg: arg.name.upcase, usage: usage_string } }}\n" >&2
= render(:examples_on_error).indent 2
> exit 1
> fi
end

>

end
end
8 changes: 7 additions & 1 deletion schemas/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
},
"tab_indent": {
"title": "tab indent",
"description": "Whether to use tabs or spaces in the the generated script\nhttps://bashly.dannyb.co/usage/settings/#tab_indent",
"description": "Whether to use tabs or spaces in the generated script\nhttps://bashly.dannyb.co/usage/settings/#tab_indent",
"type": "boolean",
"default": false
},
Expand All @@ -113,6 +113,12 @@
"type": "boolean",
"default": true
},
"show_examples_on_error": {
"title": "show examples on error",
"description": "Whether to show command examples when the input line is missing required arguments\nhttps://bashly.dannyb.co/usage/settings/#show_examples_on_error",
"type": "boolean",
"default": true
},
"env": {
"title": "env",
"description": "Whether to include development related comments in the generated script\nhttps://bashly.dannyb.co/usage/settings/#env",
Expand Down
7 changes: 7 additions & 0 deletions schemas/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
"minLength": 1,
"default": "Allowed: %{values}"
},
"examples_caption_on_error": {
"title": "examples caption on error",
"description": "The string to show before the examples when show_examples_on_error is enabled\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
"type": "string",
"minLength": 1,
"default": "examples:"
},
"help_flag_text": {
"title": "help flag text",
"description": "The help message for --help\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
Expand Down
21 changes: 21 additions & 0 deletions spec/approvals/examples/command-examples-on-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
+ bashly generate
creating user files in src
created src/download_command.sh
created src/upload_command.sh
created ./cli
run ./cli --help to test your bash script
+ ./cli download
missing required argument: SOURCE
usage: cli download SOURCE [TARGET] [OPTIONS]
examples:
cli download example.com
cli download example.com ./output -f
+ ./cli upload
missing required argument: SOURCE
usage: cli upload SOURCE [OPTIONS]
examples:
Upload a file
$ cli upload profile.png -u admin -p s3cr3t

Upload a file (you will be prompted to provide a password)
$ cli upload profile.png --user admin
1 change: 0 additions & 1 deletion spec/bashly/script/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@
expect(subject.default_command.usage_string).to eq 'cli [get]'
end
end

end

describe '#user_file_path' do
Expand Down