Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:31:00+00:00 2026-06-09T04:31:00+00:00

I would like to parse long options in a shell script. POSIX only provides

  • 0

I would like to parse long options in a shell script. POSIX only provides getopts to parse single letter options. Does anyone know of a portable (POSIX) way to implement long option parsing in the shell? I’ve looked at what autoconf does when generating configure scripts, but the result is far from elegant. I can live with accepting only the full spellings of long options. Single letter options should still be allowed, possibly in groups.

I’m thinking of a shell function taking a space separated list of args of the form option[=flags], where the flags indicate that the option takes an arg or can be specified multiple times. Unlike its C counterpart there is no need to distinguish between strings, integers and floats.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:31:02+00:00Added an answer on June 9, 2026 at 4:31 am

    Design notes towards a portable shell getopt_long command

    I have a program getoptx which works with single-letter options (hence it is not the answer to your problem), but it handles arguments with spaces correctly, which the original getopt command (as opposed to the shell built-in getopts) does not. The specification in the source code says:

    /*
    ** Usage: eval set -- $(getoptx abc: "$@")
    **        eval set -- $(getoptx abc: -a -c 'a b c' -b abc 'd e f')
    ** The positional parameters are:
    ** $1 = "-a"
    ** $2 = "-c"
    ** $3 = "a b c"
    ** $4 = "-b"
    ** $5 = "--"
    ** $6 = "abc"
    ** $7 = "d e f"
    **
    ** The advantage of this over the standard getopt program is that it handles
    ** spaces and other metacharacters (including single quotes) in the option
    ** values and other arguments.  The standard code does not!  The downside is
    ** the non-standard invocation syntax compared with:
    **
    **        set -- $(getopt abc: "$@")
    */
    

    I recommend the eval set -- $(getopt_long "$optspec" "$@") notation for your getopt_long.

    One major issue with getopt_long is the complexity of the argument specification — the $optspec in the example.

    You may want to look at the notation used in the Solaris CLIP (Command Line Interface Paradigm) for the notation; it uses a single string (like the original getopt() function) to describe the options. (Google: ‘solaris clip command line interface paradigm’; using just ‘solaris clip’ gets you to video clips.)

    This material is a partial example derived from Sun’s getopt_clip():

    /*
    
    Example 2: Check Options and Arguments.
    
    The following example parses a set of command line options and prints
    messages to standard output for each option and argument that it
    encounters.
    
    This example can be expanded to be CLIP-compliant by substituting the
    long string for the optstring argument:
    
    While not encouraged by the CLIP specification, multiple long-option
    aliases can also be assigned as shown in the following example:
    
    :a(ascii)b(binary):(in-file)(input)o:(outfile)(output)V(version)?(help)
    
    */
    
    static const char *arg0 = 0;
    
    static void print_help(void)
    {
        printf("Usage: %s [-a][-b][-V][-?][-f file][-o file][path ...]\n", arg0);
        printf("Usage: %s [-ascii][-binary][-version][-in-file file][-out-file file][path ...]\n", arg0);
        exit(0);
    }
    
    static const char optstr[] =
        ":a(ascii)b(binary)f:(in-file)o:(out-file)V(version)?(help)";
    
    int main(int argc, char **argv)
    {
        int c;
        char *filename;
    
        arg0 = argv[0];
        while ((c = getopt_clip(argc, argv, optstr)) != -1)
        {
            ...
        }
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to parse a string like this: -o 1 --long Some long
In a C++ program, I would like to have a long-only option with a
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
I would like to parse the following text into formatted URLs with anchor tags:
I would like to parse the folder name and file name from passed argument.
I would like to parse the values from aspx pages, both what is passed
I would like to parse free-text time intervals like the following, using Python: 1
I would like to parse through and display and picture and description for each
I'm new to jQuery and would like to parse an XML document. I'm able
I use JSON.NET and I would like to parse the following object which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.