Today we are going to talk a little about creating command-line applications using #Python and its built-in `argparse` package!
๐งต๐๐
In this example of using `argparse`, you don't even create any custom arguments! You just create an ArgumentParser argument and call `print_help()`!
You can see in this example that `argparse` already provides useful information to your users without adding any arguments!
Normally when you create an `ArgumentParser` object, you would do so in a function and return the object.
Let's rewrite the previous example to follow that coding pattern:
Now let's add a required argument and a couple of optional arguments to our Python ArgumentParser!
To make an argument required, set `required=True`
Now let's try running your argparse example. Here are a few different examples of running your #Python command line app:
The previous examples only showed how to add short named arguments.
Here is an example that shows how you can have both a short name and a full or long named argument with `argparse`:
#Python's argparse module also supports creating mutually exclusive arguments. ๐๐ฅ
Here is an example:
You can learn more about using Python's `argparse` by checking out my tutorial on @mousevspython
blog.pythonlibrary.org/2015/10/08/a-iโฆ
@mousevspython A popular alternative to using #Python's built-in `argparse` module is a package called Click.
Click uses decorators and makes things even easier.
You can learn more here:
click.palletsprojects.com/en/8.0.x/
@mousevspython You may also want to check out the Python Fire package. It is a different kind of for package for automatically generating command line interfaces.
google.github.io/python-fire/
@mousevspython Thanks for reading my thread on creating command-line interfaces with #Python!
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.