1

I have a function with a few parameters. For example:

makeUser{
login
email
password}

I want to make flags like -l|--login, -e|--email and -p|--password but I don't know how.

The sample for it should look like the code below:

./script.sh --mode makeUser --login test --email test@test.com -p testxx

How can I achieve that result? I know only how to make it with getopts (short flags).

Should it look like code below?

while true ; do
case "$1" in
    -m|--mode)
        makeUser)
           case "$2" in
              -l|--login)
                 makeUser "$OPTARG"
                    case "$3" in
                       -e|--email)
                           makeUser "$OPTARG"
                              case "$4" in
                                 -p|--password)
                                     makeUser "$OPTARG"
                                     exit $?
                               esac ;;
                    exit $?
                    esac ;;       
            exit $?
            esac ;;    
        makeProject)...
        makeSite)...
    exit $?    
    esac ;;
done
geckon
  • 8,316
  • 4
  • 35
  • 59
adamos
  • 69
  • 8
  • 2
    well, all the options should be in the same level of indentation. – fedorqui Aug 07 '15 at 12:37
  • Could you make an answer and show it to me ? What about prefixes ( -l|--login , -e|--email , -p|--password) ? – adamos Aug 07 '15 at 12:39
  • This should be helpful: http://stackoverflow.com/questions/4180880/how-to-support-both-short-and-long-options-at-the-same-time-in-bash – Cyclonecode Aug 07 '15 at 12:40
  • Do any of those options have reasonable default values? If not, you should keep them as positional arguments, not options. If you only want the options for their descriptive nature, you can require arguments of the form `login=test email=test@example.com password=testxx` and parse them in your script. (Which is easy to do; I can post an answer with the details.) – chepner Aug 07 '15 at 13:08
  • The one reason which is important that they should be on their positions. For example i have method: assignProject where i need variables : user + project + access level and there is a method makeUser where i need login+email+password. If you could post in answer some code with the details i would be grateful! Thanks!! – adamos Aug 07 '15 at 13:14

1 Answers1

1

Using while and shift makes a clean solution for getopt-alike behaviour in bash:

while [ $# -gt 0 ]; do
    case "$1" in
        -h|"-?"|--help)
            shift
            echo "usage: $0 [-v] [--mode MODE] [-l login] [-e email] [...]"
            exit 0
            ;;
        --mode)
            MODE=$2
            shift; shift;
            ;;
        -l|--login)
            LOGIN=$2
            shift; shift;
            ;;
        -e|--email)
            EMAIL=$2
            shift; shift;
            ;;
        -v|--verbose)
            VERBOSE=1
            shift;
            ;;
         *)
            echo "Error: unknown option '$1'"
            exit 1
        esac
done

# example function makeUser
makeUser()
{
    login=$1
    email=$2

    echo "function makeUser with login=${login} and email=${email}"
}


if [ "$MODE" == "makeUser" ]; then
    makeUser $LOGIN $EMAIL # ... and so on
fi
redimp
  • 1,061
  • 8
  • 12
  • Okey but how to set that one parameter should be 3rd ? I mean concatenation to know that first parameter to makeUser is login , then email not password and last is password? I can't get it – adamos Aug 07 '15 at 12:51
  • it should not make a difference if the script is called with `--mode makeUser --login test` or `--login test --mode makeUser` should it? – redimp Aug 07 '15 at 12:57
  • i am using curl option later so i have got specified sequence of parameters in each function that's why i am asking. My another question is how does shift really works? Because i made this script with getops ( and it works ) but i didnt use shift option. – adamos Aug 07 '15 at 12:59
  • 1
    I updated the answer, to better match your questions. hth. – redimp Aug 07 '15 at 13:02
  • I gonna try it, thank you! – adamos Aug 07 '15 at 13:03
  • Okey, how can i connect $login and $email with parameters in method makeUser ? I have got makeUser{ login = $1, email = $2 and so on ... } – adamos Aug 07 '15 at 13:10
  • I removed the 2nd answer here and fixed the copy n paste error with the 2x LOGIN – redimp Aug 10 '15 at 12:00
  • Okey i see this. How can i connect $LOGIN or $EMAIL with variable1=$1 and variable2=$2 from function makeUser ? Because it doesn't see it. – adamos Aug 10 '15 at 12:04
  • 1
    I added an example function that takes two arguments. – redimp Aug 10 '15 at 16:01