andsens / docopt.sh Goto Github PK
View Code? Open in Web Editor NEWCommand-line argument parser for bash 3.2, 4+, and 5+.
License: MIT License
Command-line argument parser for bash 3.2, 4+, and 5+.
License: MIT License
Reproduce:
#!/usr/bin/env bash
docopt_repro() {
DOC='
docopt.sh bug reproduce
Usage:
docopt-repro [-a] command
docopt-repro [-a]
'
eval "$(docopt "$@")"
return
}
docopt_repro "$@"
Thanks a lot for this script! Really useful.
I use it on Centos7 / Summit and shasum is not there. You should use instead sha256
.
Thanks again,
Thomas
PS: This is the fix I implemented. I can submit a PR if you want
if [[ -x "$(command -v shasum)" ]]; then
doc_hash=$(printf "%s" "$DOC" | shasum -a 256)
elif [[ -x "$(command -v shas256sum)" ]]; then
doc_hash=$(printf "%s" "$DOC" | shasum)
fi
When passing escaped variables in the DOC they are not escaped once output is assigned.
I've just found DOCOPT and I love it!
This is just a question, maybe even a nonsense :)
I was wondering if is there any pattern to use docopt.sh as a project dependency or on contrary is this kind of use discouraged. I couldn't manage to make it work in this way.
I mean, include docopt.sh repository as a Git submodule into another project to allow regenerating the parser on-the-fly and reacting to changes on the $DOC
without requiring neither to install docopt separately via pip3
, nor run it explicitly from command-line ($ docopt.sh SCRIPT
).
The aim would be to be able to use someting like: eval "$(./relative/path/to/docopt.sh --parser "$0")"
in a script, as the docopt.sh would be present as Git submodule.
If I'm not wrong, this would keep the script clean from parser-generated code. Sure for this to work, the script should live in its Git repository to maintain access to the parser (so no portable script). The aim would only be to allow other developers to quick modify the DOC options & run transparently without extra steps, always shipping the script as a Git repository.
Options may switch places in the AST at times, resulting differing output for the same $DOC
. This is likely because dict
is used, OrderDict
should be used instead.
Detection of DOC=
should be able to handle single quotes as well as double quotes.
docopt.sh should allow indented DOC=
strings and detect other script parts that are indented as well, so that commands that are just functions (e.g. included in .bashrc) don't require weird indentation levels.
docopt_run()
exits too late when the source
command fails with docopt_parse: command not found
. Make sure to wrap the source
in an if and exit earlier.
I think it would nice to have a built-in mechanism to dump all recognized options and their parsed values. This could be a function included with the parser (say if DOCOPT_INCLUDE_DEBUG=true). I think it will be helpful to someone inexperienced in bash scripting, as well as a good way to show what variables are being defined etc. I would suggest including the option name, the variable name, and its value.
Hi there!
I'm not very sure if the following should be ever possible, but we faced with this little problem and thought may worth to open an issue :)
We'd like to tell somehow, that the generated parser stops processing options. I don't know if using a delimiter like --
could help.
The use case is an script that's using docopt.sh
to parse just a piece of the options given, and internally is calling an underlying command with some other options passed to the script's CLI:
resticw -p profileX stats latest --cache-dir=/tmp/cache
The goal would be to separate somehow the options that are for the outer script (the ones that need to be parsed), to the rest of the line. So in the above example, only the -p profileX
option would be parseable by docopt.sh
, while the rest should not.
When running the above command, we get the usage help error, as in the example --cache-dir
is not something defined as an option for the script (full DOC below).
The following works just fine:
resticw -p profileX stats latest
Thanks!
DOC="A little wrapper over restic just to handle profiles and environment loading.
Usage:
resticw [options] <restic_arguments_line>...
The <restic_arguments_line> is just the regular unwrapped restic command arguments, e.g. stats latest
Options:
-p --profile=<name> Specify the profile to load or use default [default: default].
Examples:
resticw --profile profileA snapshots
resticw stats latest # this will use the profile: default
"
When including docopt as a lib SC1090 should be ignored. More often than not the source is non-constant.
Because the doc is trimmed and the digest is generated from the trimmed version, appending to the doc will not cause the doc check to fail.
The solution is to use the raw $DOC
Characters like `
need to be escaped with a \
in double quoted docs. This will cause docopt.sh to generate the wrong hash on parser generation. The docstring should be parsed instead and then hashed.
The regular expression used to find the DOC string does not account for escaped quotes of any kind. Figure out if regex is still the way to go, or if a more reliable alternative should be implemented instead.
When declaring array vars SC2154 must be disabled, since they are not declared through an eval meaning shellcheck can discover our trickery.
stderr from shasum should be quenched unless the doc check fails.
It seems your parser doesn't use python docopt for parsing bash $DOC
I also realized than docopt installed from pip install docopt
(0.6.2) is different from the code available on github.
Could you please explain the way you parse bash $DOC
usage from bash script, and the goal of each class?
I started to analyze you code, parsing:
Script
object which use a Doc
object to extract docopt part from the bash source script
Doc()
extracts bash $DOC
string which is matched from this regexpDoc
holds extracted part of the docopt stringScript()
object then passes the doc
instance to Guards
object which I don't know the role yet.How do you install without pip?
Thank you for this library, it is really useful.
It was a bit surprising that -h
and --help
both work, but not -v
or -V
in addition to --version
. Would be nice for the short version to be supported as well.
Following docopt/docopts#35 : this is a twin issue with the project docopts - twin docopt/docopts#36
docopts
use can use global vars with or without prefix the names in the option are converted to mangled names (code here) (which is compatible to old python's output, old code here)
Current docopt.sh code
docopts README describing the manged argument conversion.
In order to improve compatibility we need to output compatible variables name from docopt.sh
Usage could be:
DOC="
Usage: pipo new [--switch <value>]
"
DOCOPT_COMPAT_DOCOPTS=mangled
docopt "$@"
compatible Outputs:
new=true
switch=true
value='some_value'
0.9.16 causes the shellcheck disable=
codes to be output e.g. like this # shellcheck disable=2016,10901091,2034
docopt.sh version: 0.9.17
Hi @andsens, I've observed that when setting the following in scripts that are using docopt.sh, they fail:
set -u
or usually
set -euo pipefail
I tried to reproduce it with the demo in the README, so just setting the above flag after the shebang, then run and the following error shows when the script is run:
❯ ./naval.sh
./naval.sh: line 85: best_match_idx: unbound variable
./naval.sh: line 125: move: unbound variable
❯ ./naval.sh Olympia move 1 5 --speed 8
./naval.sh: line 82: match_count: unbound variable
./naval.sh: line 125: move: unbound variable
I'm usually adding the set -eu ...
as good practice for scripts. Is that incompatible in some sense with docopt or is it an issue?
Cheers!
Running docopt over this help string:
DOC="Upload Tool.
Usage:
./upload.sh [-h]
Options:
-h Print this help string
"
I'm getting a littany of shellcheck errors. SC1009 for a parse error on "parse_long". SC1056 for a "expected }". An undefined error on failure to parse this line left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
.
Despite these errors, the script runs fine, so either the minification confuses shellcheck. Unsure how to resolve if this is a shellcheck problem unable to properly parse the minified parser.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.