Comments (14)
Not yet - if you can wait a little longer (hours not days), I will have a release with args as well.
For now, you can use the same Gemfile, only use branch master
.
I am also going to change the implementation a little (internals only) to be easier to maintain, and to add the args. Should be ready real soon.
from bashly.
I am happy to hear that other people make good use of bashly, thanks for sharing that.
While I can see the value of what you are proposing, my initial reaction is that this is something that is better handled by the application space, rather than the framework space.
Why I think it should be handled by the user-domain application
Note that even in your question, you wanted to show a friendly error to the user:
Invalid role - allowed values are "admin" and "user"
So, if this kind of feature is handled by the framework, it will require doing quite a few things. Such as, handle the help message accordingly, perhaps alter the usage text, add the same kind of feature to the positional arguments, and possibly more.
Also keep in mind, one of bashly's objectives is to be easy to learn, and easy to configure. I intentionally created a minimal set of tools - so much so, that some "features" were excvluded and can be opt in by calling bashly add FEATURE
.
How I propose to do it (without any change to bashly).
I think you can still implement what you want, with ease, and without "copy pasting" code around. You can even do so in a way that is easy to then copy the solution between your different scripts.
Here is what I suggest:
Step 1: Initialize a script workspace with functions lib
$ bashly init --minimal
$ bashly add lib # this just creates a sample function in `./sec/lib`
Step 2: Update bashly.yml with a sample flag+argument
# bashly.yml
name: login
help: Sample minimal application without commands
version: 0.1.0
args:
- name: site
help: Site to login to
flags:
- long: --user
short: -u
arg: NAME
help: "User name. Allowed: user, admin"
Step 3: Create the verify_user
function
# src/lib/verify_user.sh
verify_user() {
allowed="$1"
if [[ ! ${args[--user]} =~ $allowed ]]; then
echo "Invalid role. Must be one of $allowed"
exit 1
fi
}
Step 4: Use the new user "filter" anywhere
For example, in the root command:
# src/lib/root_command.sh
verify_user "admin|user"
inspect_args
Keep in mind, I am not totally against such a feature, and while I am sure it will add value, the question is at what cost, and what does it take away in return (e.g. configuration simplicity).
How do you feel about that?
from bashly.
@DannyBen : Thanks for the reply. I already make use of library scripts to make this work and it does the job just fine. Since Bashly already provides this flexibility, I am fine with not bundling this into the framework if it increases complexity.
from bashly.
Ok - let's keep this open for now, so we can think about it a little.
If you have further details or suggestions on implementation (for example, error message configuration - needed or not?), please share.
from bashly.
A generic message template along the lines of Invalid option - allowed options are ...
should be okay for me. Of course, message templating would be nice to have.
from bashly.
Of course, message templating would be nice to have.
Alright. You are familiar with bashly add strings
yes?
This lets you customize all of bashly's text output.
So, if we implement this feature, the message will at least be customizable in the same way - although not sure it will be able to say "Invalid role" - since this means it must be configured in the bashly.yml.
from bashly.
I think we can perhaps add this functionality without any major disruption.
Will you be able to test that it fits your use case if I create a branch with it?
Are you using the Ruby gem or the docker version?
from bashly.
Currently, I'm using the Rubygem version but I am fine with testing with the docker version separately as well.
from bashly.
Excellent. Actually its easier for both of us to test the gem directly.
I will hopefully post a branch and instructions later.
from bashly.
Alright - I believe it is ready for your second opinion.
To test it (either in a new empty folder, or directly in your bashly workspace):
Create this Gemfile
# Gemfile
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "bashly", github: 'DannyBen/bashly', branch: 'add/flag-whitelist'
Run bundle
, and you are ready to test.
You can use this initial bashly.yml
if you want:
# bashly.yml
name: login
help: Sample minimal application without commands
version: 0.1.0
args:
- name: site
help: Site to login to
flags:
- long: --user
short: -u
arg: NAME
allowed: [user, admin]
# required: true
default: user
help: User name
You can see more details in the PR notes
from bashly.
Tested the change and works perfectly fine for flags
Any chance if the same could be made for "args"?
from bashly.
Any chance if the same could be made for "args"?
Funny. I was about to ask you if you think it is needed there as well.
I think adding it should be easy - let me take a look at it, perhaps add it to this PR.
from bashly.
I merged the PR to master, and opened a new issue to track the args implementation.
from bashly.
Thanks a lot - I didn't check yet but was the Rubygem updated with the feature for flags already?
from bashly.
Related Issues (20)
- Presence of `header.sh` breaks executable HOT 5
- How to access permanent environment variable in ./zshrc or ./bashrc HOT 3
- Automation Tools
- test/approve template missing HOT 3
- How can I install this on CentOS ? HOT 1
- Allow flags for commands that have subcommands HOT 20
- Flags not shown in help
- Is there a way to add alias "help" to fixed_flags_filter ? HOT 5
- Is there a way to put cli_usage to custom header? HOT 1
- Dafault environment_variables should be accessible in initialize script HOT 3
- Send errors to stderr HOT 7
- Upgrade template & utility functions HOT 3
- Only commands can be imported? HOT 4
- Is there a way to create global flags? HOT 11
- Issues with `normalize_input` and `catch_all` HOT 14
- Ambiguous error messages HOT 8
- Make invalid `expose` warn instead of error HOT 8
- Error when splitting configuration in sub/nested commands HOT 5
- Alias a command to a subcommand HOT 20
- Ambiguous error messages HOT 11
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.