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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:24:11+00:00 2026-05-24T19:24:11+00:00

We have a simple Python program to manage various types of in-house servers, using

  • 0

We have a simple Python program to manage various types of in-house servers, using argparse:

manage_servers.py <operation> <type_of_server> 

Operations are things like check, build, deploy, configure, verify etc.

Types of server are just different types of inhouse servers we use.

We have a generic server class, then specific types that inherit from that:

class Server
    def configure_logging(self, loggin_file):
        ...
    def check(self):
        ...
    def deploy(self):
        ...
    def configure(self):
        ...
    def __init__(self, hostname):
        self.hostname = hostname
        logging = self.configure_logging(LOG_FILENAME)
class SpamServer(Server):
    def check(self):
        ...
class HamServer(Server):
    def deploy(self):
        ...

My question is how to link that all up to argparse?

Originally, I was using argparse subparses for the operations (check, build, deploy) and another argument for the type.

subparsers = parser.add_subparsers(help='The operation that you want to run on the server.')
parser_check = subparsers.add_parser('check', help='Check that the server has been setup correctly.')
parser_build = subparsers.add_parser('build', help='Download and build a copy of the execution stack.')
parser_build.add_argument('-r', '--revision', help='SVN revision to build from.')
...
parser.add_argument('type_of_server', action='store', choices=types_of_servers,
                    help='The type of server you wish to create.')

Normally, you’d link each subparse to a method – and then pass in the type_of_server as an argument. However, that’s slightly backwards due to the classes- I need to create an instance of the appropriate Server class, then call the operation method inside of that.

Any ideas of how I could achieve the above? Perhaps a different design pattern for Servers? Or a way to still use argparse as is?

Cheers,
Victor

  • 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-05-24T19:24:12+00:00Added an answer on May 24, 2026 at 7:24 pm

    Just use the parser.add_subparsers(dest=... argument with a mapping of type_of_server to classes:

    subparsers = parser.add_subparsers(dest='operation', help='The operation that you want to run on the server.')
    ...
    server_types = dict(spam=SpamServer, ham=HamServer)
    args = parser.parse_args()
    server = server_types[args.type_of_server]()
    getattr(server, args.operation)(args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple program using parallel python, and all works well. However,
I have a simple python program that I'd like to daemonize. Since the point
I have been trying to create a simple program with Python which uses OpenCV
Simple case I have a Python program that I intend to support on both
I have written a simple python program l=[1,2,3,0,0,1] for i in range(0,len(l)): if l[i]==0:
I have a simple script blah.py (using Python 2): import sys print sys.argv[1] If
I have a simple python program and I want an executable version (for Ubuntu
I've written a simple GUI program in python using Tkinter. Let's call this program
I have a simple Python program which uses a read-eval-print loop to read user
Beginner programmer here. So bear with me. I have a simple python program. print

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.