Coder Social home page Coder Social logo

Comments (9)

barberj avatar barberj commented on June 8, 2024

I was just having this conversation with @halst .

I second the idea. If sys is called within the method and not in the method header I can test like a user would call it...

https://github.com/barberj/ForGit/blob/5aedc5afeb3112796cb55f3e5f571d1c4e580938/test_forgit.py (Specifically look at TestCLI)

Above is an example of the tests I'm able to achieve using my forked docopt with argv defaulted to none.

from docopt.

keleshev avatar keleshev commented on June 8, 2024

Why not pass sys.argv[1:] explicitly in case something modifies sys.argv. The benefits of this change are not clear to me.

from docopt.

barberj avatar barberj commented on June 8, 2024

I'm indifferent now (Line 180 https://github.com/barberj/ForGit/blob/master/forgit.py). I have been doing just as you suggested. It was just frustrating initially when i was trying to write test cases.

from docopt.

schmir avatar schmir commented on June 8, 2024

The current behaviour is surprising and unexpected. To quote https://en.wikipedia.org/wiki/Principle_of_least_astonishment:

"People are part of the system. The design should match the user's experience, expectations, and mental models."

If you know about the current behaviour, it may be easy to workaround. But that's not a good reason to keep it and I can't think of other reasons to keep the current behaviour...

btw. when can we expect the next release?

from docopt.

keleshev avatar keleshev commented on June 8, 2024

Next release is planned this year.

Let me be devil's advocate for a moment:

What is so astonishing/unexpected when docopt parses the exact same options that were passed from command-line, not those obtained by some side-effect?

from docopt.

schmir avatar schmir commented on June 8, 2024

docopt doesn't parse the options that were passed from command-line. it parses the value of sys.argv at import time.
given the fact that some people will mock with sys.argv, docopt's behaviour is now also dependent on the import order.

and if you're one of those, you'll have to look around a bit in order to explain docopts behaviour. it isn't obvious that it's parsing something different from the value of sys.argv at call time.

python's behaviour of evaluating default arguments at import time is also a well known gotcha and unexpected to some people (see http://stackoverflow.com/questions/530530/python-2-x-gotchas-and-landmines). you're relying on that exact feature in order to implement more unexpected behaviour.

speaking of side-effects I think I can always just reload(docopt) in case I need to call into a program's main function :)

from docopt.

keleshev avatar keleshev commented on June 8, 2024

I'm sold. This will break some people's code, but I'm implementing that for purity sake. Import order should not influence docopt behavior.

from docopt.

maximerischard avatar maximerischard commented on June 8, 2024

Reading sys.argv at runtime also makes it possible to use docopt interactively with ipython's run command.

from docopt.

keleshev avatar keleshev commented on June 8, 2024

As of 0.6.0, argv is None by default, and docopt evaluates sys.argv[1:] when called. Thanks everyone for the fruitful discussion.

from docopt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.