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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:43:59+00:00 2026-05-23T09:43:59+00:00

I’m writing a python command-line tool using the cmd2 module which is an enhanced

  • 0

I’m writing a python command-line tool using the cmd2 module which is an enhanced version of cmd. The tool is an administration application and is used to administer a number of sub-modules.

I’d like to structure the code so that each sub-module has its own class responsible for providing the commands that can be executed on that module. But I’m not sure how to structure this given how the command class works.

So what I’d like is something like:

import cmd

class ConsoleApp(cmd.Cmd):

    # this is the main app and should get the commands from the sub-modules

app = ConsoleApp()
app.cmdloop()

And then the sub-modules would be separately defined.

class SubModuleA():

    def do_sm_a_command_1(self, line):
        print "sm a command 1"

class SubModuleB():

    def do_sm_b_command_1(self, line):
        print "sm b command 2"

How can I structure this so that the main app will pick up the commands from the sub-modules?

Thanks, Jon

  • 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-23T09:44:00+00:00Added an answer on May 23, 2026 at 9:44 am

    You may have some luck structuring your SubModules as Plugins to the main ConsoleApp. You’d need a couple of new methods on ConsoleApp. Something like add_command_module(self, klass) which would just append the klass (SubModuleA for example) to some list inside ConsoleApp.

    Then in ConsoleApp, override the onecmd method to look something like this

    def onecmd(self, line):
      if not line:
        return self.emptyline()
      if cmd is None:
        return self.default(line)
      self.lastcmd = line
      if cmd == '': 
        return self.default(line)
      else:
        # iterate over all submodules that have been added including ourselves
        # self.submodules would just be a list or set of all submodules as mentioned above
        func = None
        for submod in self.submodules:
          # just return the first match we find
          if hasattr(submod, 'do_%s' % cmd):
            func = getattr(submod, 'do_%s' % cmd)
            break # instead of breaking, you could also add up all the modules that have
                  # this method, and tell the user to be more specific
        if func is not None:
          return func(arg)
        else:
          return self.default(line)
    

    You could also hack up the parseline method to recognize submodule prefixes for commands etc…

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

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.