Module to read input from commandline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • james@reggieband.com

    Module to read input from commandline

    Hi all,

    I did some quick searching but I haven't found anything like I want.
    It probably has to do with the terms I am searching for so if I
    describe what I want then I hope someone can point me to a good
    module.

    I want to take input from the user at the command line. e.g.)
    Would you like to continue [Y/n]: y
    What is your favorite color: green
    .....

    You get the idea. Basic question answer stuff. It should handle
    default options, case insensitivity, etc. Perhaps the module would
    compare the inputted text against a regexp for validation. If the
    module had an interface similar to OptParse that would be nice.

    Anyone know of a decent module that handles this type of thing?
    Writing from scratch would be simple but why re-invent the wheel.

    Cheers,
    James.
  • thashooski@farifluset.mailexpire.com

    #2
    Re: Module to read input from commandline

    What you're looking for is no module, it is included in the standard
    python namespace.

    raw_input

    Use it like this:

    value_a = raw_input("Plea se give a value for a: ")
    # do your thing to value_a

    But this belongs to the real basics, I suggest you get some reading
    done on python.

    GL

    Comment

    • james@reggieband.com

      #3
      Re: Module to read input from commandline

      On Apr 13, 7:44 pm, thashoo...@fari fluset.mailexpi re.com wrote:
      What you're looking for is no module, it is included in the standard
      python namespace.
      >
      raw_input
      >
      Use it like this:
      >
      value_a = raw_input("Plea se give a value for a: ")
      # do your thing to value_a
      >
      But this belongs to the real basics, I suggest you get some reading
      done on python.
      >
      GL
      Thanks for the response GL.

      What I am looking for is a module to wrap raw_input so it can handle
      some of the validation.

      e.g.)
      response = display_prompt( question, valid_responses ,
      default_respons e )

      or similar. valid_responses could be a tuple of regexp strings that
      would compare against a raw_input and return one in the list. Ideally
      I could customize the error message (for responses not in
      valid_response) , etc.

      I know it isn't rocket science to write it and I have something
      already in place. I'd rather use a module built for the purpose now
      that I have several scripts that will use the functionality.

      Thanks!
      James.

      FYI: This is what I have so far:

      from re import compile

      def yes_no(question , default=None):
      """Prompt the user with a yes or no question."""
      if default == "y":
      question = "".join((questi on, " [Y/n]: "))
      elif default == "n":
      question = "".join((questi on, " [y/N]: "))
      else:
      question = "".join((questi on, " [y/n]: "))

      valid_answer = "[yn]"
      invalid_message = "Answer must be y or n"
      answer = prompt(question , valid_answer, invalid_message , default)
      return answer == 'y'

      def prompt(question , valid_answer, invalid_message , default=None):
      """Prompt the user with a question and validate their response."""
      is_valid = False;
      compiled_valid_ answers = compile(valid_a nswer)
      while not is_valid:
      answer = raw_input(quest ion).lower()
      if answer == "" and default is not None:
      answer = default
      is_valid = compiled_valid_ answers.match(a nswer)
      if not is_valid:
      print invalid_message
      return answer

      Comment

      • james@reggieband.com

        #4
        Re: Module to read input from commandline

        On Apr 13, 9:05 pm, ja...@reggieban d.com wrote:
        On Apr 13, 7:44 pm, thashoo...@fari fluset.mailexpi re.com wrote:
        >
        What you're looking for is no module, it is included in the standard
        python namespace.
        <snip>
        raw_input
        >
        Thanks for the response GL.
        >
        What I am looking for is a module to wrap raw_input so it can handle
        some of the validation.
        Looks like I found the module after-all:

        Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha...


        The cmd module does what I want and more.

        Just wanted to post this in-case someone finds this through a search.

        cheers,
        James

        Comment

        Working...