0
  1. I wish to pass long and short CLI options. I have tried below for short options:

    #!/bin/bash
    while getopts d:t:r: option 
    do
     case "${option}"
     in
     d) c_date=${OPTARG};;
     t) c_type=${OPTARG};;
     r) c_date_range=${OPTARG};;
     esac
    done
    shift $((OPTIND -1))
    
    echo "Date:"$c_date
    echo "Type:"$c_type
    echo "Range:"$c_date_range

How can I accept long options like --date, --type, --range?

  1. The range option accepts 2 dates i.e. -r 2019-01-01 2019-02-01 or --range 2019-01-01 2019-02-01. How can I accept these dates and save in separate variables?
S R
  • 657
  • 3
  • 10
  • 21
  • Look into getopt --longoptions - it will give you building block for parsing long options. Look at: https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options – dash-o Oct 07 '19 at 18:37
  • 1
    Unfortunately, there is no support for 'getlong' to take 2 values (as per the range). Common alternative are (1) have two flags ('--from date --to date). (2) combine the two date into single parameter (2019-01-01:2019-02-01), and decode the range or (3) modify the case for 'r' to pick the next argument (explicitly move OPTIND). not easy, but possible – dash-o Oct 07 '19 at 18:41

0 Answers0