shflags icon indicating copy to clipboard operation
shflags copied to clipboard

Is there a way pass undefined params and the shell script will not FATAL and exit

Open dblate opened this issue 2 years ago • 2 comments

Sometimes, we may pass some undefined params to shell script, is there a way(such as a flag) to ignore the undefined params, and the script will run success? Now the script will have a FATAL error and exit when we pass undefined params

dblate avatar Apr 21 '23 09:04 dblate

This is covered in the Quick Start, but maybe that could be improved or documented elsewhere. Where did you look (and not find) an answer?

This is a copy-and-paste from the current 1.3 docs (linked above). I've bolded the line relevant for you. Let me know if you have improvement suggestions.

# parse the command-line
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

Here is where all the magic happens. The call to FLAGS with the passing of $@ sends all command-line arguments into the shFlags library for processing. If everything is successful, a return value of 0 (${FLAGS_TRUE}) is returned an things continue. If a false value of 1 (${FLAGS_FALSE}) or an error value of 2 (${FLAGS_ERROR}) is returned, the processing is halted, returning that value (the exit $?). If there are any command-line arguments left over that shFlags didn't know how to process, they are now present in the now updated $@ variable, which can be used as with any other script.

kward avatar Aug 21 '23 12:08 kward

Thanks for your answer, but that's not my situation. In my case, for example, examples/hello_world.sh, we can run as :

$ ./hello_world.sh -n Kate
Hello, Kate!

But if I add a -c which is not defined in hello_world.sh, it will raise a error:

$ ./hello_world.sh -n Kate -c example
flags:WARN getopt: invalid option -- 'c'
 -n 'Kate' -- 'example'
flags:FATAL unable to parse provided options with getopt.

Is there a way not raise a error in this situation?

dblate avatar Oct 20 '23 11:10 dblate