getopts | Node.js CLI options parser | Command Line Interface library
kandi X-RAY | getopts Summary
kandi X-RAY | getopts Summary
Node.js CLI options parser
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of getopts
getopts Key Features
getopts Examples and Code Snippets
Community Discussions
Trending Discussions on getopts
QUESTION
So I'm writing a shell-script that takes optional arguments (parsing them with getopts
and what not; but that doesn't really matter here).
The user (of the script) is supposed to pass some paths as arguments, which are then processed by some workhorse utilitiy.
Of course the paths may contain spaces, so I'm passing the args on with double-quotes ("$@"
), along with numerous other flags:
something like this:
...ANSWER
Answered 2021-May-31 at 08:28If you have control over how default_paths
gets set in your script, then you can just use set
to assign the paths as positional arguments
QUESTION
I am attempting to pass an argument via getopts which is a file, ie ./file.txt or file.txt. The problem is, a function that is using the getopts arg isn't interpreting it properly, and shows as an empty arg when debugging. Code shown below:
...ANSWER
Answered 2021-May-12 at 14:46You failed to declare either -t
or -p
as taking an argument, so OPTARG
isn't set for either option.
getopts optstring name [args]
getopts is used by shell procedures to parse positional parameters. optstring contains the option characters to be recognized; if a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.
A correct call to getopts
would be
QUESTION
we're currently using pushover, to send messages to our Servicedesk, when our Monitoring finds Errors within our systems.
Our system collects the data, creates a message, and then sends the needed arguments to a perl script:
...ANSWER
Answered 2021-Mar-31 at 13:03IT seems that Pushover migrated their API to different IP addresses in order to counteract a DDoS attack on it.
Since our firewall was only configured to allow traffic to the old addresses, our System was unable to send messages to the IP.
Why LWP::Useragent flagged this as "invalid arguments" is currently beyond me, but the problem at hand is currently solved.
QUESTION
I'm using the following Bash version in a up-to-date CentOS 7 VM:
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
The following code performs as expected (take note of -x -y):
...ANSWER
Answered 2021-Mar-29 at 23:49How can this be remedied in Bash?
Well, by patching the sources. But I wouldn't like to do it, I believe the current behavior is the right one.
You could add an additional function to getopt.c
and expose it via some special variable in variables.c
that would allow manipulating getopts
internal state.
Or even simpler - I see getopts.def loadable builtin which you could patch to add some additional option to serialize/deserialize getopts state.
And you can also provide your own implementation of getopts
as a bash function with your custom semantics and custom state serializer/deserializer.
Can anyone shed any light on
From posix getopts:
If the application sets OPTIND to the value 1, a new set of parameters can be used: either the current positional parameters or new arg values. Any other attempt to invoke getopts multiple times in a single shell execution environment with parameters (positional parameters or arg operands) that are not the same in all invocations, or with an OPTIND value modified to be a value other than 1, produces unspecified results.
From that we know:
- setting OPTIND=1 resets the internal state of getopt ()
- it is not specified what should happen if you modify
OPTIND
.
The behavior you are seeing is documented - setting OPTIND=1
as $OPTIND
is equal to 1
resets getopt()
, which results in endless loop, as one would expect. Except for that, bash documentation does not specify what should happen when you modify OPTIND
. Do not do it. Your expectation that setting OPTIND
to custom value will affect getopts
in specific ways is not based on anything. It will not.
resolve this one issue when writing my wrapper to allow for partial argument/nested getopts handling?
If you are writing your own argument parsing module, do not use getopts
and do not depend on undefined, unspecified nor implementation defined behavior. I suggest to do it the same way GNU getopt
does - produce a shell source-able string in separate sub-process, instead of relying on global variables OPT*
and contributing to spaghetti code.
Do not nest getopts
, it is not re-entrant and there is no way to affect it's internal state and it uses global variables. getopts
sets OPTIND
, it is not required to read it, except for the case when OPTIND
is reset to 1
, in which case getopts
is reset. Any other value may just be ignored. Just call one getopts
after another.
QUESTION
I'm using getopts to get optional arguments in a shell script, and then I use them to run another script. one of the arguments I'd like to send to the other program is an optional argument (e.g --conf).
but when I'm sending it to getopts i get these error messages:
...ANSWER
Answered 2021-Mar-29 at 04:54Got it! After printing the args as @user1934428 suggested, I've figured out that the last argument is available to the script but not to getopts.
I've tried to understand why and it hit me, a colon is missing after the 'o'!
Instead of while getopts 'p:o' OPTION
it should be while getopts 'p:o:' OPTION
as noted in getopts man page:
if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.
Note: with the information above I fixed the issue and getopts got the last argument, though it parsed it by spaces, even though it was quoted.
It because i sent arguments to main function like this main $@
when i shouldv'e quoted it like this main "$@"
.
QUESTION
My current Bash script looks like in the following. So far it's working except I have no idea how to make it so the two options -f and -o work together:
- the -o FILE option to display the most relevant information in a file passed as an argument (if the file does not exist an error message must be displayed)
- the option -f KEYWORD to display the lines containing the KEYWORD to from a file. This option must be used with the -o option
Thank you for any input
...ANSWER
Answered 2021-Mar-26 at 20:09Try this: while processing commnand line options, just collect variables. Only do things with those variables after the options are parsed.
QUESTION
I want to run the script as ./script speed -a some_value -b some_value
also ./script accuracy -a some_value -b some_value
What I tried is
...ANSWER
Answered 2021-Mar-26 at 05:48I don't think you want the outer loop (while [ -n "$1" ]; do
), unless you want to be able to process multiple subcommands in a single run. That is, do you want this:
QUESTION
I have a scenario where i would like to assign an option a default value but a user can decide to give it another argument:
Here is an example
...ANSWER
Answered 2021-Mar-19 at 10:12Is there a way in bash script to have an option to give an argument but it shouldn't a must?
Yes, there is a way.
getopts
does not supports optional arguments. So... you can:
- roll your own bash library for parsing arguments or
- use another tool that has support for optional arguments.
A common tool is getopt
that should be available on any linux.
QUESTION
I would like to be able to run a script like this ( with sub-arguments ): I need to be able to use short and long options
./myscript.sh -u myname --delete-config --delete-data
./myscript.sh -a myname ..........................
./myscript.sh -h
Actually i have :
...ANSWER
Answered 2021-Mar-11 at 17:36Not working solution, it only works in this case, not with 3 sub parameters
Thanks to Philippe for his comment : Let me know if you think it's not ok, or purpose better and a confirm as answer :)
QUESTION
I have a bash script that finds connected devices and logs the serial port output to a file. I am using getopts to choose the method of logging. Two of the functions are the same except for one line. I would like to change it to a single function with a variable for the single line that is different (my attempt is below) but have had no success. How can I do this with getopts? Many thanks.
...ANSWER
Answered 2021-Feb-11 at 01:28I changed a couple things, because I realized that explaining all little details in comments would be too long.
Here is the code:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install getopts
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page