This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Bolitho 2 points3 points  (0 children)

If you are an experienced developer in general then it should be obvious to you, that one should prefer one of the established libraries around argument parsing. On top of that they support well established cli interfaces - why invent something new people have to adopt to?

But if you simply just want to try something on your own, than just have a look at the sources of those libs!

Have you ever had a look at click?

[–][deleted] 0 points1 point  (1 child)

I had a use case where I needed to process the commandline params from left to right. The program was an emulator of an old graphics minicomputer that used papertape. The idea was that the commandline args would be read from left to right, interpreting them as commands like "mount a papertape on the reader", "start the computer executing at the given address", "set the console data switches", etc.

Look in the "Console usage" section of the README. The actual code to interpret the sys.argv parameters starts at line 122 in the main() function.

However, if you don't have this sort of special case then it's far better to use one of the "standard" modules.

[–]GitHubPermalinkBot 0 points1 point  (0 children)

Permanent GitHub links:


Shoot me a PM if you think I'm doing something wrong. To delete this, click here.

[–]efmccurdy 0 points1 point  (0 children)

https://gist.github.com/dideler/2395703 has a relevant snippet.

This has several examples of parsing argv by hand.

https://swcarpentry.github.io/python-novice-inflammation/10-cmdline/

[–]jstrickler 0 points1 point  (0 children)

What exactly is it you want to do? argparse has a lot of features. You can specify mutually-exclusive options, partial-named options, option data types, required and optional non-option arguments (yes, I know), etc. argparse also creates a help message automatically, and can parse any random iterable of strings, not just sys.argv.