Coder Social home page Coder Social logo

Comments (6)

DannyBen avatar DannyBen commented on June 21, 2024 3

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.

DannyBen avatar DannyBen commented on June 21, 2024 2

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.

DannyBen avatar DannyBen commented on June 21, 2024
  1. This is very nonnstandard. Can you share examples of one or two notable command line tools that do it?
  2. This requires a nested dictionary as the receiver: args[--flag][flagKey]=flagValue, which does not exist in bash.

from bashly.

user-name-is-taken avatar user-name-is-taken commented on June 21, 2024

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.

user-name-is-taken avatar user-name-is-taken commented on June 21, 2024

As far as I see it there are 2 good options for how to receive the input:

  1. As environment variables
  2. 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 do command --flag value1,value2,value3 instead of command --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.

DannyBen avatar DannyBen commented on June 21, 2024

No, none of these are good in my opinion.

  1. 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.
  2. 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)

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.