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

  • SEARCH
  • Home
  • 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 8901051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:15:12+00:00 2026-06-15T01:15:12+00:00

Is there a good (ideally CPAN) way to process arbitrary command line options with

  • 0

Is there a good (ideally CPAN) way to process arbitrary command line options with Perl?

E.g., take a string "-a 1 -b 'z -x' -c -d 3 4" and generate a GetOpt::Long – like data structure:

 { a=>1, b=>"z -x", c=>1, d=>[3,4] }  # d=>"3 4" is acceptable

The caveat is that

  1. the set of options is NOT known in advance; so we seemingly can’t use GetOpt::Long as-is.

  2. The values themselves can contain other options, so simply parsing the string for #\b+-(\S+)\b# pattern to find all the options in THAT string also seems to not be possible, and further complicated that some parameters are of =s type, some =s@, some of “-x a b c” type.

  3. Moreover, even if we could do #2, does GetOptionsFromString support correct tokenizing that respects quoted values?


NOTE: Assume for the purpose of exercise that ALL arguments are “options”, in other words, if you split up the string into (possibly-quoted) tokens, your structure is always

 "-opt1 arg1a [arg1b] -opt2 ....".

In other words, any word/token that starts with a dash is a new option, and all the subsequent words/tokens that do NOT start with a dash are values for that option.

  • 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-15T01:15:14+00:00Added an answer on June 15, 2026 at 1:15 am

    A quick example using Text::Parsewords and a simple state machine.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use Text::ParseWords qw/shellwords/;
    
    my $str = q{-a 1 -b 'z -x' -c -d 3 4};
    my $data = parse($str);
    
    use Data::Printer;
    p $data;
    
    sub parse {
      my $str = shift;
    
      my @tokens = shellwords $str;
    
      my %data;
      my @keys;
      my $key = '_unknown';
      foreach my $token (@tokens) {
        if ($token =~ s/^\-//) {
          $key = $token;
          push @keys, $key;
          next;
        }
    
        if ( ref $data{$key} ) {
          push @{ $data{$key} }, $token;
        } elsif (defined $data{$key}) {
          $data{$key} = [ $data{$key}, $token ];
        } else {
          $data{$key} = $token;
        }
      }
    
      foreach my $key (@keys) {
        next if defined $data{$key};
        $data{$key} = 1;
      }
    
      return \%data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a good way of test if a string is a regex or
Is there any good library which supports WebSockets and is compatible with GWT? Ideally,
Are there any good technical comparisons available between the various game development options available.
Is there a good way to keep keys from conflicting when using the Microsoft
Is there a good way to start at a given index and move BACKWARDS
Are there any good tools to make css sprites? IDEALLY I'd want to give
Are there any good scriptable installer/uninstaller systems made for Java projects? Ideally a system
In PSQL, is there a good way of finding all the tables that inherit
Is there a good way to .appendTo either one element or the either depending
Are there any good multi-tenant Azure sample applications? Ideally I'd like to see some

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.