-
-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow command line flags to be used in any order #1
Comments
From my understanding
I initially thought that was the issue, but after changing the order of the flags in the |
From my experience, |
Ah yes, you are correct. I think |
This can also be accomplished by looping over the arguments and while true ; do
case ${1:-} in
--help | -\?)
usage
exit 0
;;
-v | --verbose)
# Each instance of -v adds 1 to verbosity
verbose=$((verbose+1))
shift
;;
-q | --quiet)
quiet="1"
shift
;;
--) # End of all options
shift
break
;;
-*)
echo "FATAL: Unknown option : $1" >&2
exit 1
;;
*) # no more options. Stop while loop
break
;;
esac
done |
I think I've come to a conclusion as to why this this happening and here is the best explanation that I'm able to come up with: I written a very minimal code below to show what is happening when we RAW_TEXT=false
view_recent_email(){
echo "The value of RAW_TEXT is --> $RAW_TEXT" # [Step 2]
}
while [[ "$1" ]]; do
case "$1" in
--recent|-r) view_recent_email ;; # [Step 1]
--text|-t) RAW_TEXT=true ;; # [Step 3]
esac
shift
done Output:
Why is the value for Because our first argument was The value of But, when we run RAW_TEXT=false
view_recent_email(){
echo "The value of RAW_TEXT is --> $RAW_TEXT" # [Step 3]
}
while [[ "$1" ]]; do
case "$1" in
--recent|-r) view_recent_email ;; # [Step 2]
--text|-t) RAW_TEXT=true ;; # [Step 1]
esac
shift
done Output:
So Conclusion: We might need to change the code a little so it looks something like this: RAW_TEXT=false
VIEW_RECENT_EMAIL=false
view_recent_email(){
echo "The value of RAW_TEXT is --> $RAW_TEXT"
}
while [[ "$1" ]]; do
case "$1" in
--recent|-r) VIEW_RECENT_EMAIL=true ;;
--text|-t) RAW_TEXT=true ;;
esac
shift
done
if [ $VIEW_RECENT_EMAIL = true ];then
view_recent_email
fi Outputs: $ bash script.sh --recent --text
The value of RAW_TEXT is --> true
$ bash script.sh --text --recent
The value of RAW_TEXT is --> true This allows us to use any order of the flags because the Hope this explanation was understandable |
Edit: I misread this, I will try again after my coffee |
Yes, this is a good approach. If you ever have parameters that themselves take an argument then you will want to do the |
I have a branch in my fork in which I changed the command syntax and re-wrote the option parsing. Before I request a pull, I think I'll propose my changes here. The new command syntax, which is documented in a man page (tmpmail.1), is thus:
Note that the options are presented in alphabetical order. I have reserved lower case -v for verbose mode and used upper case -V for version, as is common. I think that the options parsing code is more clear and detects problems better:
What do you think? |
I think that this style is easier to read. What do you think?
|
-b | --browser)
[ -z "$2" ] && print_error "missing arg for $1" ||
BROWSER="$2"; shift ;; I think you will need |
Incorrect. You're forgetting the shift that is the last statement of the while loop. |
True. I'm not used to doing it that way, but it should work. |
At the moment the flags have to be in a certain order.
For example if I want to view the most recent email and want the output to be as raw text, I have to run
tmpmail --text --recent
for it work properly. Runningtmpmail --recent --text
will ignore the--text
flag.By allowing the flags to be used in any order would make it much easier to use.
The text was updated successfully, but these errors were encountered: