docopt / docopts

Shell interpreter for docopt, the command-line interface description language.
Other
504 stars 53 forks source link

Do you have an idea for representing key/value options? #77

Open kendalldita opened 1 month ago

kendalldita commented 1 month ago

Java and other software has key/value pair parameters, e.g. the lang property:

java -Dlang=en -cp some.jar input output

Another example:

xsltproc --param lang en id.xsl in.xml

The parameter name can be anything. Could something like that be represented in docopt syntax?

Sylvain303 commented 1 month ago

@kendalldita did you read http://docopt.org/ ?

Your questions seems not specifically related to docopts bash parser but more about the docopt (without S) language, right?

You should also paste your code you're trying, in order to complete your question, if related to docopts. For more general docopt question probably stackoverflow would be a better place to ask.

https://stackoverflow.com/questions/tagged/docopt

Here is a sample with your input:

#!/usr/bin/env bash
# Usage: xsltproc --param=PARAM INPUT OUTPUT
#        xsltproc --lang=LANG INPUT OUTPUT                                                                                            
#

# no PATH changes required if docopts binary is in the PATH already
PATH=..:$PATH
source ../docopts.sh
HELP=$(docopt_get_help_string $0)
version='0.1'

parsed=$(docopts -A myargs -h "$HELP" -V $version : "$@")
#echo "$parsed"
eval "$parsed"

# main code
for a in ${!myargs[@]} ; do
    echo "$a = ${myargs[$a]}"
done

some use-case:

$  ./xsltproc.sh 
error: 
Usage: xsltproc --param=PARAM INPUT OUTPUT
       xsltproc --lang=LANG INPUT OUTPUT

with string parameter --param do what ever you want with.

$  ./xsltproc.sh --param='lang=en' id.xsl in.xml
OUTPUT = in.xml
--param = lang=en
INPUT = id.xsl

better would be to let docopts parse it by introducing a --lang parameter.

$ ./xsltproc.sh --lang=en id.xsl in.xml
OUTPUT = in.xml
--param = 
INPUT = id.xsl
--lang = en
kendalldita commented 1 month ago

Yes. In general, I use software that tries to conform to standards, and try to find an audience that is also interested in both conforming to standards, and practical usages of those standards, while the vast majority of people only care about what is in front of their face at the moment. Then questions are shunted away to different environments designed to avoid anyone asking questions.

Thanks for enduring questions this far. I'll now go get my questions rejected on stack overflow.

Sylvain303 commented 1 month ago

Oh, sorry to read that your question was rejected on stackoverflow. 😒

There's way to ask for help, especially in open source and free software community, you may learn with interacting with friendly or not-so-friendly people. 😉

I posted on stackoverflow yesterday for testing, following our conversation.

https://stackoverflow.com/questions/69292099/docopt-required-flag-where-the-value-for-it-can-only-be-one-of-a-few-options#new-answer

You should notice as I mentioned about docopt:

You should, may be, read more carefully how people get answer for there question. What you could improve on what I saw on the issues we exchanged is:

Yes, this is long and sometime disappointing, sometime very grateful 🤷‍♀️