bash - Why cant I have a space between option and optional argument using getopt? -


When using the getopt to parse the command line parameter, you can add arguments between the option flag and the required argument , But for this to be corrected after alternative logic options, alternative arguments will only be parsed.

  TEMP = `getopt -op: q :: -n 'mkqueue.sh' -" $ @ "` If [$? ! = 0]; Then "Termin ... ..." & gt; & Amp; 2; Exit 1; Note the quotation marks around Fi # $$: they are important! Eval set - "$ TEMP" # Now is the right to go through all the options; In the case of "$ 1" -p) echo "option p; logic \ '$ 2'"; Shift 2 ;; -q) "echo" in "$ 2"), "option q, no logic"; Shift 2 ;; *) Echo "option q, argument \ '$ 2'"; Shift 2 ;; ASAC ;; --) Shift ; break ;; *) Echo "Internal Error!" ; Exit 1 ;; Esac  

(based on this):

When I run the script without optional arguments, then it works:

  ./ Mkqueue.sh -p adfsa -q option p, logic `adfsa 'option q, no logic  

If I try to add alternative logic to the location in the -q I:

  ./ mkqueue.sh -p adfsa -q sdfasdfa option p, logic `adfsa 'option q, no argument  

this job If you do not have a place in the middle of don option and logic, even if the expected logic is a Work with Triksh:

 . / Mkqueue.sh -p adfsa -qsdfasdfa option p, logic 'adfsa' option q, logic `sdfasdfa ' 

Is there a fix for this?

this limitation getopt :

A simple short option is entered in the Mainpad for `- 'A short option occurs after the character if the option contains the required argument, then this option is followed by the character or next parameter (i.e. different from the white space on the command line) Can be written directly in the form. If the option has an alternative argument, then this option should be written directly after the character.

Although getopt allows "alternative option logic", they are generally considered a bad idea, for example, in Pozzix Includes:

Guideline 7 : Option-argument should not be optional.

The root cause for this direction is that the argument can be any string in command-line option from the convention. This may sound like another option, for example it could be a literal string - , which usually indicates the end of the options list. It can be an empty string anything.

The problem with "Optional Option Argument" is that an alternate option argument is not provided that the following argument is any option or - But this means that the value of the alternative argument can not begin with - .

Alternatively, you use the getopt command-line utility, for which alternative logic immediately follows the option, without any spacing (which is in the same word) without. But in that case, the alternative argument can not be an empty string.

Instead of creating complex rules to solve these ambiguities, the simplest solution - recommended by Pozxix (and, with little authority by me) - is not to allow alternative arguments to be optional if command- If there is a general value for line option and you want to simplify the user's life by not needing to type it, then apply two different command line options, one of which And who else does not do this. Common techniques include using a lower-case letter for the version without reason, and with that argument, the corresponding upper case for the version or a longer option for that version ( - option ) Is included.

For those people who interpret dense and packed descriptions, Pausix points out that:

Guideline 7 allows any string to be substance-logic; An alternative-logic can begin with any character, it may be - or - , and an empty string can be found. For example, PR-h - , public relations- h - , public relations-HDD , PR- H + 2 < / Code>, and the option-logic - , - , -d , in PR -h " Code> +2 , and an empty string, respectively. On the contrary, the command public relations-a-D treats as an option, not in the form of an argument, because - is an option-logic here, not the delimiter .


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -