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 8777487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:17:25+00:00 2026-06-13T19:17:25+00:00

How can I parse variable length argument lists delimited by special predefined syntax. An

  • 0

How can I parse variable length argument lists delimited by special predefined syntax. An example:

   ./script --arg1 --cmdname otherscript --a1 --a2 --cmdname-- --arg3

After parsing with argparse script should have three arguments: arg1, cmdname, arg3. The argument cmdname should consist of a list of three values otherscript, a1, a2.

Having such a recipe would be useful to be able to pass on everything in cmdname into a subprocess.popen(cmdname, ...) call.

I was thinking about subparsers. But I believe a subparser cannot be stopped, and really is mutually exclusive with other subparsers. Any other easy, already provided way? Is subclassing the Action the way to do it?

  • 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-13T19:17:27+00:00Added an answer on June 13, 2026 at 7:17 pm

    As you indicated in your post, subclassing Action is probably the way to do this — Although that gets pretty tricky if the arguments to otherscript aren’t known by argparse. You might be able to get around this with parse_known_args, but you might not. Honestly, I really think the easiest way is to preprocess sys.argv yourself.

    import shlex
    s = shlex.split("./script --arg1 --cmdname otherscript --a1 --a2 --cmdname-- --arg3")
    def preprocess(lst):
        """
        process an iterable into 2 lists.
        The second list contains the portion bracketed by '--cmdname' and '--cmdname--'
        whereas the first portion contains the rest of it.
        """
        argv1,argv2 = [],[]
        current = argv1
        for i in lst:
            if i == '--cmdname':
               current = argv2
            elif i == '--cmdname--':
               current = argv1
            else:
               current.append(i)
        return argv1,argv2
    
    l1,l2 = preprocess(s)
    print l1
    print l2
    

    And an alternative implementation of preprocess which works for sliceable objects that have a .index method — sys.argv would work just fine:

    def preprocess(lst):
        """
        process an iterable into 2 lists.
        The second list contains the portion bracketed by '--cmdname' and '--cmdname--'
        whereas the first portion contains the rest of it.
        """
        try:
            i1 = lst.index('--cmdname')
            i2 = lst.index('--cmdname--')
            argv1 = lst[i1+1:i2]
            argv2 = lst[:i1]+lst[i2+1:]
        except ValueError:
            argv1 = lst
            argv2 = []
    
        return argv1,argv2
    

    Another option (pointed out in an excellent comment by @unutbu) is to change the commandline syntax to something a little more standard which simplifies the problem greatly:

    ./script --arg1 --cmd "otherscript --a1 --a2" --arg3
    

    Then you can parse cmd as you normally would using argparse (specify type=shlex.split for this argument to convert from a string to a list of arguments).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been attempting to write a VBA Script that can parse out other
How would you parse a variable length sequence of bytes where first bit (BigEndian)
How i can parse and extract the parameters from an SQL Query using delphi?
Is there a Python library that can parse an CSS selector and emit an
Does anybody know a Perl library that can parse XML documents and enables me
Does a tool exist that can parse text and output that text, hyper-linked to
I need a PHP Regex that can parse .strings files. In particular, it needs
I'm using RSS library so i can parse Atom and RSS in Ruby and
I need a function that can parse both 1,123.12 and 1.123,12 to 1123.12 There
Is there any tool that can parse/conver xml files to this format? I have

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.