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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:34:17+00:00 2026-06-14T06:34:17+00:00

Currently I’m creating a directory reader program using Python. I’m using ‘argparse’ to parse

  • 0

Currently I’m creating a directory reader program using Python.
I’m using ‘argparse’ to parse the arguments from command line. I have the following code:

parser = argparse.ArgumentParser(prog = "LS.py",
                                 usage = "%(prog)s [options] [path1 [path2 [...pathN]]]\nThe paths are optional; if not given . is used.")

group = parser.add_argument_group("Options")

group.add_argument("-path", default = ".", help = argparse.SUPPRESS, metavar = "")
group.add_argument("-m", "--modified", default = False,
                    help = "show last modified date/time [default: off]",
                    metavar = "")
group.add_argument("-o ORDER", "--order=ORDER", nargs = 2, default = "name",
                    help = "order by ('name', 'n', 'modified', 'm', 'size', 's')\n[default: name]",
                    metavar = "")
group.add_argument("-r", "--recursive", default = False,
                    help = "recurse into subdirectories [default: off]",
                    metavar = "")
group.add_argument("-s", "--sizes", default = False,
                   help = "show sizes [default: off]", metavar = "")

args = parser.parse_args()
return args

When called in the following manner “LS.py -h” it produces the following output:

usage: LS.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.

optional arguments:
  -h, --help            show this help message and exit

Options:
  -m , --modified       show last modified date/time [default: off]
  -o ORDER  , --order=ORDER  
                    order by ('name', 'n', 'modified', 'm', 'size', 's')
                    [default: name]
  -r , --recursive      recurse into subdirectories [default: off]
  -s , --sizes          show sizes [default: off]

My question: Is there a way to move the default help argument into a group such as Options?
Also, I can’t seem to find a way to remove the space before the commas in the Options arguments. The ideal output is:

Usage: ls.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.

Options:
  -h, --help            show this help message and exit
  -m, --modified        show last modified date/time [default: off]
  -o ORDER, --order=ORDER
                        order by ('name', 'n', 'modified', 'm', 'size', 's')
                        [default: name]
  -r, --recursive       recurse into subdirectories [default: off]
  -s, --sizes           show sizes [default: off]
  • 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-14T06:34:18+00:00Added an answer on June 14, 2026 at 6:34 am

    You can use add_help=False to disable the built-in help command and add your own instead, using action="help" (thanks @mgilson!)

    To get rid of the spaces, don’t set metavar to an empty string. Your options should be specified using action="store_true" to make them true (argument-less) options:

    import argparse
    
    parser = argparse.ArgumentParser(prog="LS.py",
                                     usage="%(prog)s [options] [paths...]\nThe paths are optional; if not given . is used.",
                                     add_help=False)
    
    group = parser.add_argument_group("Options")
    
    group.add_argument("-h", "--help", action="help", help="show this help message and exit")
    group.add_argument("-path", default=".", help=argparse.SUPPRESS)
    group.add_argument("-m", "--modified", action="store_true",
                        help="show last modified date/time")
    group.add_argument("-o", "--order", nargs=1, default="name",
                        help="sort order (n[ame], m[odified], s[ize])\n[default: name]")
    group.add_argument("-r", "--recursive", action="store_true",
                        help="recurse into subdirectories")
    group.add_argument("-s", "--sizes", action="store_true",
                       help="show sizes")
    
    args = parser.parse_args()
    

    Output:

    Options:
      -h, --help            show this help message and exit
      -m, --modified        show last modified date/time
      -o ORDER, --order ORDER
                            sort order (n[ame], m[odified], s[ize]) [default:
                            name]
      -r, --recursive       recurse into subdirectories
      -s, --sizes           show sizes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am creating VBA Macros for Excel and recently have started using UserForms.
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
currently i have <div class=rightBoxesTop> <h3>My Pages</h3> <h3 style=line-height: 8px; width: 80px; font-size: 80%;
Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
Currently I'm using the Jackson JSON Processor to write preference data and whatnot to
Currently playing about with KnockoutJS. Just trying to update an observable array from a
Currently i am consuming web service from android by the method SOAP.Here i need
Currently writing a C# application it should do backups using GIT in the background.

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.