I think you probably need:
  sys.argv = ['script', -sName]

argv[0] is, by long convention, the command name.

Randolph



On Oct 7, 2013, at 4:22 PM, Maria McKinley wrote:

> I couldn't get either the sys.argv.extend or sys.argv = [None...] to work. I 
> just keep getting the error to specify that option. They do specify it kind 
> of strangely. the option is script.py -sName
> which I've never seen before. They just kind of run the option name and the 
> argument together. I tried all kinds of variations 
> '-sname' 
> '-s', 'name'
> '-s=name'
> 's=name'
> 
> Nothing works. This library doesn't seem to have an option for running it 
> directly from the python API. I'm thinking their library was not tested with 
> actual test code. Meh.
>  
> I'm contemplating mucking with their code to set a default for their 
> 'mandatory' option, but I'd really rather not do that.
> 
> And yes, testing it as a script would not be unit testing. I'm not sure what 
> I was imagining there. ;-)
> 
> thanks,
> Maria
> 
> 
> On Mon, Oct 7, 2013 at 4:08 PM, Matt S. <[email protected]> wrote:
> Chris beat me to the punch but here's what I was thinking...
> 
> http://docs.python.org/2/library/optparse.html
> 
> 15.5.3.7 Parsing arguments
>     (options, args) = parser.parse_args(args=None, values=None) 
>  
> where the input parameters are
>    args -- the list of arguments to process (default: sys.argv[1:])
> 
> It looks to me like you just need to emulate sys.argv[1:].
> 
> ./myscript.py -a val1 -b val2
> 
> Maybe a bad idea but I think if you import sys and then define sys.argv as a 
> list whose first item is going to be ignored:
> 
> if __name__=='__main__':
>     sys.argv = [None, "-a", "val1", "-b", "val2"]
> 
>     start()
> 
> 
> 
> 
> 
> 
> On Mon, Oct 7, 2013 at 3:31 PM, Maria McKinley <[email protected]> 
> wrote:
> I am using a python package that has a required command line option I am 
> suppose to use when I invoke my script if I am using their library. Kind of 
> weird, I know, I love required options! I want to load my class from the 
> python interpreter for testing purposes. Is there a way to specify options 
> from the python interpreter? I know that __main__ is run from the command 
> line, so I should be able to add stuff in an if statement that checks to see 
> if __main__ was called, and before I run the method start(), which loads 
> their stuff, but not sure what. I also know that they are using optparse to 
> check the options.
> 
> This is for unit testing, so alternatively if there is a sensible way to unit 
> test scripts with command line options, that would work, but that seems 
> trickier. And honestly, the way this package is set up, not sure how far I'm 
> really going to get with unit testing, at all. :-(
> 
> And no, in case you are wondering, I'm not excited about using this package, 
> but I don't have a choice in the matter. For the curious, here is the package.
> 
> http://memory.psych.upenn.edu/PandaEPL
> 
> Thanks,
> Maria
> 
> 
> 
> 
> -- 
> Maria Mckinley
> Software Developer with Bonus SysAdmin Experience
> www.mariakathryn.net
> www.linkedin.com/in/mariamckinley

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to