3

I'm writing my first python command line tool using docopt and have run into an issue.

My structure is like this:

Usage:
  my-tool configure
  my-tool [(-o <option> | --option <option>)]
  ...

I'm trying to find a way to run my-tool -o foo-bar first, and then optionally pass the value 'foo-bar' into my configure function if I run my-tool configure next.

In pseduocode, that translates to this:

def configure(option=None):
    print option # With the above inputs this should print 'foo-bar'

def main():
    if arguments['configure']:
       configure(option=arguments['<option>'])
       return
    ...

Is there a way to get this working without changing the argument structure? I'm looking for a way to avoid my-tool configure [(-o <option> | --option <option>)]

intboolstring
  • 6,891
  • 5
  • 30
  • 44
mdegges
  • 963
  • 3
  • 18
  • 39
  • how about update the option before calling the `configure` function? e.g., read the config file to get the previous command options, and then update the `option` variable to include the `-o` option if exist, then pass it to the `configure` function. – Enix Oct 13 '16 at 05:53
  • The question is *how* to get previous CL arguments. When running the second command 'my-tool configure', – mdegges Oct 13 '16 at 14:59

2 Answers2

1

Since you run this on 2 different instances it might be best to store the values in some sort of config/json file that will be cleared each time you run "configure".

import json

def configure(config_file):
   print config_file[opt_name] # do something with options in file

   # clear config file
   with open("CONFIG_FILE.JSON", "wb") as f: config = json.dump([], f)

def main():
    # load config file
    with open("CONFIG_FILE.JSON", "rb") as f: config = json.load(f)

    # use the configure opt only when called and supply the config json to it
    if sys.argv[0] == ['configure']:
       configure(config)
       return

    # parse options example (a bit raw, and should be done in different method anyway)
    parser = OptionParser()
    parser.add_option("-q", action="store_false", dest="verbose")
    config_file["q"] = OPTION_VALUE
Blackbeard
  • 107
  • 3
  • Hmm yeah, this looks like it will work. I'll give it try. Was just hoping there was a cleaner way to do it without having to create an external file or set an env variable. – mdegges Oct 13 '16 at 15:01
  • you can refactor it a bit and write the whole thing as a singleton. Then, for as long as the singleton is running in the background (or until another 'configure' command is triggered), you'd be able to trigger changes on the same process by calling 'configure'. This way no persistency will be needed. – Blackbeard Oct 14 '16 at 10:17
0

I tried writing some script to help you but it was a bit beyond my (current) Newbie skill level. However, the tools/approach I started taking may be able to help. Try using sys.argv (which generates a list of all arguments from when the script is run), and then using some regular expressions (import re...).

I hope this helps someone else help you. (:

maze88
  • 850
  • 2
  • 9
  • 15