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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:10:31+00:00 2026-06-13T23:10:31+00:00

tldnr: given a function, is there a way to automatically create an ArgumentParser from

  • 0

tldnr: given a function, is there a way to automatically create an ArgumentParser from its signature?

I’ve got a bunch of functions that I’d like to expose to the command line. So basically, a module:

 def copy(foo, bar, baz):
    ...
 def move(from, to):
    ...
 def unlink(parrot, nomore=True):
    ...

 if __name__ == '__main__':
     argparse stuff

which can be called from the command line like this:

 python commands.py move spam ham
 python commands.py unlink --parrot Polly

Although this is pretty straightforward to implement, there’s a lot of wiring involved:

parser = argparse.ArgumentParser(...)
subparsers = parser.add_subparsers()
...
c = subparsers.add_parser('unlink', description='Unlink a parrot')
c.add_argument('--parrot', help='parrots name', required=True)
c.add_argument('--nomore', help='this parrot is no more', action='store_true')
...
c = subparsers.add_parser('move', description='Move stuff')
...

and so on, for each function. The worst thing, should function arguments change (and they do), the argparse stuff needs to be synchronized manually.

It would be much nicer if the functions could provide argparse stuff for themselves, so that the main code would be like:

parser = argparse.ArgumentParser(...)
subparsers = parser.add_subparsers()

copy.register(subparsers)
move.register(subparsers)
unlink.register(subparsers)
...

I thought of something along these lines:

@args(
    description='Unlink a parrot',
    parrot={'required':True, 'help':'parrots name'},
    nomore={'action': 'store_true', 'help': 'this parrot is no more'}
)
def unlink(parrot, nomore=True):
    ...

My questions:

  • is there a library that does something like this?
  • if no, is it possible to write such a decorator, and how?
  • is there a other/better way to implement what I want?

Upd:

plac appears to be the solution. Here’s how to do what I want with plac:

commands module: cmds.py:

import plac

@plac.annotations(
    foo=('the foo thing'),
    bar=('the bar thing'),
    fast=('do a fast copy', 'flag')
)
def copy(foo, bar, fast=False):
    """Copy some foo to bar."""
    pass
        
@plac.annotations(
    parrots=('parrots names'),
    nomore=('these parrots are no more', 'flag'),
    repeat=('repeat n times', 'option', 'r', int)
)
def unlink(nomore=False, repeat=1, *parrots):
    """Unlink some parrots."""
    pass

#more commands...

# export commands so that plac knows about them
commands = 'copy', 'unlink'

and here’s the main module:

import plac
import cmds

plac.call(cmds)

Quite neat if you ask me.

  • 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-13T23:10:32+00:00Added an answer on June 13, 2026 at 11:10 pm

    Have you tried plac?

    An example in docs:

    # dbcli.py
    import plac
    from sqlalchemy.ext.sqlsoup import SqlSoup
    
    @plac.annotations(
        db=plac.Annotation("Connection string", type=SqlSoup),
        header=plac.Annotation("Header", 'flag', 'H'),
        sqlcmd=plac.Annotation("SQL command", 'option', 'c', str, metavar="SQL"),
        delimiter=plac.Annotation("Column separator", 'option', 'd'),
        scripts=plac.Annotation("SQL scripts"),
        )
    def main(db, header, sqlcmd, delimiter="|", *scripts):
        "A script to run queries and SQL scripts on a database"
        yield 'Working on %s' % db.bind.url
    
        if sqlcmd:
            result = db.bind.execute(sqlcmd)
            if header: # print the header
                yield delimiter.join(result.keys())
            for row in result: # print the rows
                yield delimiter.join(map(str, row))
    
        for script in scripts:
            db.bind.execute(open(script).read())
            yield 'executed %s' % script
    
    if __name__ == '__main__':
        for output in plac.call(main):
            print(output)
    

    Output:

    usage: dbcli.py [-h] [-H] [-c SQL] [-d |] db [scripts [scripts ...]]
    
    A script to run queries and SQL scripts on a database
    
    positional arguments:
      db                    Connection string
      scripts               SQL scripts
    
    optional arguments:
      -h, --help            show this help message and exit
      -H, --header          Header
      -c SQL, --sqlcmd SQL  SQL command
      -d |, --delimiter |   Column separator
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

TLDR: Does there exist reusable code for automatically reconnecting to a TCP server that
TLDR: I'm looking for a way to serve web resources (css, jpg, etc) from
TLDR I have a function that runs on the end of a pan in
I have a simple C# function I wish to use from VBA. For the
the function that I wrote below contains arrays aarr and barr to store DOM
TLDR: How do I call standard floating point code in a way that compiles
Since I got TLDR (too long didn't read) comments, I stripped 90% of this
I have an Android app that communicates with a server via asynchronous socket connection
if (reader.is_lazy()) goto tldr; I have a background thread that does some I/O-intensive background
TLDR: Started with this question simplified it after got some of it working and

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.