Comments (6)
Well - nothing in bashly prevents you from doing this. The only difference is terminology, and who is responsible for parsing the values.
From a framework perspective, your x=y pairs are values, either for a flag, or as an argument:
command argument=value --flag key=value
+------------+ +-------+
arg value flag value
Parsing these further to extract the two sides of the key=value pair is left up to you.
Bashly conforms to this standard:
- one dash for short flags
- two dashes for long flags
- everything else is an argument
and supports these alternative formats:
- -abc same as -a -b -c
- -a=arg same as -a arg
- --flag=arg same as --flag arg
As there is no data structure in bash that can hold this nested dictionary - I am curious, how do you expect to receive this user input?
In a richer language, I would expect --flag key=value
to be optionally parsed into a nested hash:
args = {
'--flag' => {
'key' => 'value'
}
}
# or in other words:
args['--flag']['key'] = 'value'
but this cannot be done in bash.
At any rate, I believe you should use one of these options:
- library functions - to define this type of parsing in one place, as a reusable function.
- function hooks - in case you always want to run something before function execution (like additional parsing).
- custom filters - in case you wish to validate the input is in the format you want them to be, with a filter that is not already supported natively.
from bashly.
I have created the key-value-pairs example to demonstrate how to easily support key=value pairs either for arguments or flags.
Hope this helps.
from bashly.
- This is very nonnstandard. Can you share examples of one or two notable command line tools that do it?
- This requires a nested dictionary as the receiver:
args[--flag][flagKey]=flagValue
, which does not exist in bash.
from bashly.
Here are some examples:
gcc --param name=value -D name=definition -A predicate=answer
objcopy --set-section-flags sectionpattern=flags # AND MANY OTHERS
ps -o pid=X,comm=Y
openssh -header name=value
ssh-keygen -M generate -O bits=2048 moduli-2048.candidates
python -X utf8=0
awk -v var=value -W random=num -W sprintf=num
kubectl config view -o jsonpath='{.users[].name}'
docker [container|network|plugin] ls --filter key=value
docker run --label key=value
docker volume create --driver local --opt type=btrfs --opt device=/dev/sda2
curl --data-urlencode name=val https://example.com
blkid --match-token NAME=value
man --regex -K "\-[a-z]+ [a-z]+\="
will show a more exhaustive list.
Key-value argument options I feel are more common, but are also more difficult to search for. Here are some examples I could think of:
env key=value
set
systemd-notify MAINPID=$PID
Parsing to a dictionary may be unnecessary. Allowing the user to set environment variables as done in the example commands is sufficient for what I'm doing and seems to be standard.
from bashly.
As far as I see it there are 2 good options for how to receive the input:
- As environment variables
- As an array of [key,value,key,value].... This option may be better because it would allow you to do something like
command --flag key=value,key=value,key=value
which would also be useful for people who want to docommand --flag value1,value2,value3
instead ofcommand --flag value1 --flag value2 --flag value3
. If you did this you would probably want to add a "delimiter" option for people who don't want to use commas as their delimiter.
from bashly.
No, none of these are good in my opinion.
- Environment variable is just a string - further parsing will be warranted, so you better parse the value of
${args['--flag']}
as it was provided already by bashly. - This is yet another invented structure that requires users of bashly to understand, and yet again parse themselves.
Nothing stops you from having any value (with equal signs, commas) in bashly. However, how you parse it and how you store it is up to you, and should be quite easy, especially using one of the facilities I mentioned earlier.
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 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.