Comments (8)
but I think it would be nice to have a way to do this by convention.
I thought about this, but decided against.
In Rails, here is a concept of before
and after
filter, which is similar to what you are describing - since this is a "before" filter.
I found this convention to be more trouble than its worth, and it "gets in your way" more often than not.
The straight forward solution would be to add a library function (bashly add lib
), and call it from any command file that needs it.
That said, your use case of using --debug
to set -x
indeed shows the weak spot of the decision to avoid some sort of "before" filter.
Let me sleep on it.
from bashly.
I found the issue where this was considered originally: #116
I wonder if adding optional before.sh
and after.sh
(similar to how initialize.sh
works) should be reconsidered.
Meaning - if such files are found, they will be executed in the appropriate place, then you can simply do
# src/before.sh
if [[ ${args[--debug]} ]]; then
set -x
fi
from bashly.
Thanks for raising this issue Wes.
This feature is now released in 1.0.2
Docs:
- Writing Your Scripts > Before/after hooks
- Advanced Features > Before/After Hooks
- Examples > Before/After Hooks Example
from bashly.
Thank you.
- Well, when you have subcommands, the
root_command
will never be called, this is why it is not there. - As you noticed, the
initialize
is not the place - it is done before anything else.
Defining flags on the root, just makes these flags available in all subcommands. Note however, that input order matters:
$ <command> <root flags> <subcommand> <subcommand flags and args>
With this bashly.yml
name: cli
help: Sample application
version: 0.1.0
flags:
- long: --debug
short: -d
help: Enable debug mode
commands:
- name: download
alias: d
help: Download a file
flags:
- long: --force
short: -f
help: Overwrite existing files
you get:
$ ./cli --debug download --force
args:
- ${args[--debug]} = 1
- ${args[--force]} = 1
Does this help?
Was this not intuitive/clear enough in the docs? Let me know where I can improve the behavior or documentation.
from bashly.
Yes, that's clear from the docs, which are very good, btw.
I guess what I'm asking for is some way to run code after the arguments are initialized but before the command/sub-command is invoked.
For example, if I have the --debug
flag on the root command, could there be some way to define a function that gets called?
# :flag.case
--debug)
# :flag.case_no_arg
args['--debug']=1
shift
# <========= need a function call right here
;;
My only alternative, is to check for ${args[--debug]}
in every single sub-command's *_command.sh
file. It works, but I think it would be nice to have a way to do this by convention.
from bashly.
This is now implemented in the edge version.
You can run bashly add hooks
to add the two before/after hook files, and edit them as you see fit.
from bashly.
@westhomas were you able to test it?
from bashly.
Just tested it. It's working very nicely! Thank you!
from bashly.
Related Issues (20)
- Allow default array when repeatable is true HOT 9
- Add support for array in `flag.default` and `arg.default` schema HOT 1
- JSON schema validation passes when it shouldn't HOT 12
- Add JSON schema tests to ensure the validation fails when it should HOT 1
- Whitelisted arg with default from environment HOT 6
- Add `environment_variable.allowed` whitelist HOT 1
- Update `inspect_args` to also print managed environment variables HOT 1
- Docker container does not have pandoc installed HOT 2
- Do not render `inspect_args` when `env` is set to `production` HOT 2
- Add key-value parameters HOT 6
- Add support for recursive --help HOT 6
- Build multi-arch docker HOT 2
- Default argument is not validated HOT 6
- Flag arguments with a single hyphen prefix are not working HOT 1
- bash completions can't work properly when the position args used 'allowed:' HOT 3
- Repeatable args don't escape quotation marks HOT 12
- Add option to disable splitting of flags with value for catch_all arguments HOT 9
- Lifecycle for temporary directories HOT 1
- Heredoc indentation issue in bashly generated functions. HOT 9
- The Flag value is detected as an Argument HOT 2
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.