Coder Social home page Coder Social logo

how to implement boolean flags about bashly HOT 7 CLOSED

dannyben avatar dannyben commented on May 25, 2024
how to implement boolean flags

from bashly.

Comments (7)

DannyBen avatar DannyBen commented on May 25, 2024

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:

  1. In a new folder, run bashly init -m to get a sample YAML file
  2. Replace the YAML file with the content of the file below
  3. Run bashly g to generate the bash script
  4. 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.

dkebler avatar dkebler commented on May 25, 2024

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.

dkebler avatar dkebler commented on May 25, 2024

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.

DannyBen avatar DannyBen commented on May 25, 2024

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.

DannyBen avatar DannyBen commented on May 25, 2024

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.

DannyBen avatar DannyBen commented on May 25, 2024

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.

DannyBen avatar DannyBen commented on May 25, 2024

Thanks for your input, I am closing this - feel free to comment if the issue is still not clear.

from bashly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.