Comments (7)
All flags are boolean (unless they have an argument).
For example, if you have an argument called --force
, it will be set to a truthy value (1
) to the variable ${args[--force]}
. Otherwise, this variable will not exist, meaning it will be falsey.
And yes, environment variables will work as default values, simply because whatever you provide as a default value is passed as is in the script.
Follow this example:
- In a new folder, run
bashly init -m
to get a sample YAML file - Replace the YAML file with the content of the file below
- Run
bashly g
to generate the bash script - Run
./download -f -o JSON
to see the output of your-f
boolean flag and the default value that came from an environmen variable
name: download
help: Minimal sample script
version: 0.1.0
args:
- name: source
default: $PWD
help: URL to download from
flags:
- long: --force
short: -f
help: Overwrite existing files
- long: --output
short: -o
arg: format
help: Choose output format
examples:
- download -o JSON -f
- download /tmp
You can now simply edit the src/root_command.sh
file (since this template generates a shell script with one command only), and use the boolean flag like this:
# src/root_command.sh
# This is only a shortcut that shows all input arguments
inspect_args
if ${args[--force]}; then
echo "=== force is ON"
else
echo "=== force is OFF"
fi
I usually assign the input values from the hash to their own variables, like this:
# src/root_command.sh
force=${args[--force]}
if [[ $force ]]; then
echo "=== force is ON"
else
echo "=== force is OFF"
fi
Also, for more "real world" examples, see one of the examples at the end of the README, for example, this edit command for the rush
CLI - see how it uses the input variables,
from bashly.
Thanks, Ah, I see it is using the long name as the hash key since these is no arg:
in the yaml, and using 1 for truthy. I guess that's all I needed to know.
I'd add what your wrote to the readme. Very helpful.
from bashly.
another question:
arg | If the flag requires an argument, specify its name here.
I expected bashly to use my arg: as the hash key instead it used the long name, or set a variable of that name.
so here it should use rhost as the key
flags:
- long: --host
short: -h
arg: rhost
help: remote host to receive backup
Running Backup Command
args:
- ${args[source]} = /opt/bash/shell/host/all/modules/backup
- ${args[--mirror]} = 1
- ${args[--host]} = test
expected - ${args[rhost]} = test
or a variable $rhost
being set to test
I guess I don't see the point of arg: except to let bashly know that the flag takes an argument. In which case it could be anything (e.g. 1)
from bashly.
I expected bashly to use my arg: as the hash key instead it used the long name, or set a variable of that name.
No.
The purpose of arg
here is only for display purposes, and it is by design not used as the variable name, for good reason.
Consider this usage pattern:
convert --input PATH --output PATH
If the word path
would be used as the name, it would conflict.
The long argument is always the variable name - so in this case you will have ${args[--input]}
and ${args[--output]}
.
This is compatible in concept to all other CLI frameworks I am using, and my role model in particular, is the cross language docopt parser.
You will see the flag argument when you run the script with --help
.
Finally, in your case, you should probably do something like this:
flags:
- long: --host
arg: address
this will print a nice usage string that clarifies its purpose better. In my opinion.
from bashly.
Oh - and just remembered one more purpose of the arg:
If you define an argument for a flag, and the user does not provide it in the command line, it will show a friendly error.
For example, given this YAML:
name: download
help: Sample minimal application without commands
version: 0.1.0
flags:
- long: --url
short: -u
required: true
arg: address
help: URL to download from
examples:
- download -u example.com
If you run it without the argument, you get this:
$ ./download -u
--url requires an argument: --url, -u ADDRESS
from bashly.
Updated README to provide some more hints as to using the arguments in your code:
https://github.com/DannyBen/bashly#using-the-input-arguemnts-in-your-code
from bashly.
Thanks for your input, I am closing this - feel free to comment if the issue is still not clear.
from bashly.
Related Issues (20)
- Make `bashly.yml` and `settings.yml` paths configurable HOT 1
- Command Validation misses commands named like flags HOT 2
- bashly add completions error HOT 8
- Questions about environment variables HOT 3
- Compose imported config with additional items HOT 6
- Is there any security consideration for the "bash name"? HOT 4
- Provide a third state to `strict` which omits `set -e` HOT 10
- Support for Alternate Dependencies HOT 12
- bashly about “The goal is to generate a new file and fill it with many variables” HOT 2
- Do something when a root command's flag is set HOT 8
- The `initialize` file is not loaded when using `.bash` extension
- Treat `initialize.sh` like the `before` and `after` hooks
- Automatically execute bashly generate when file change HOT 3
- Support storing variable via yaml file HOT 4
- `allowed` incompatible with `required: false` HOT 6
- Flags should not be set to their default value if not explicitly used HOT 3
- Flags must have a long form if you specify a default value HOT 3
- Whitespace not respected in here documents HOT 1
- Decide whether to use `bashly-` prefix for YAML configs HOT 6
- `bashly` completions for Bash, Fish, Zsh HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bashly.