Mike Driscoll Profile picture
Feb 18 โ€ข 11 tweets โ€ข 5 min read
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!

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Feb 17
Even if you are a beginner, you have probably used #Python's built-in `print()` function.

But let's take a few minutes to talk about the humble `print()` function and see what you might have missed!

#python_builtins_by_driscollis

๐Ÿงต๐Ÿ๐Ÿ‘‡
The `print()` function takes the following arguments:

๐Ÿ *objects - Stuff to print
๐Ÿ sep=' ' - A string to separate object
๐Ÿ end='\n' - A string to end on
๐Ÿ file=sys.stdout - Where to write the data
๐Ÿ flush=False - Whether or not to force flush the stream
Let's look at some examples of using Python's `print()` function:
Read 5 tweets
Feb 17
#Python has an awesome library called `collections`! One of my favorite tools from that library is the `Counter` class.

Here I use `Counter` to find the most common occurrences of a value in a list:
Let's use `collections.Counter()` to count letters in a string!

Note that the object that is returned by the Counter class can be accessed like a dictionary!
Python's `collections.Counter()` class has special methods that you can use to `subtract()` items from the counter object.

Here's an example:
Read 4 tweets
Feb 16
If you want to use #Python to read and write text or binary files, then you'll need to familiarize yourself with the built-in `open()` function!

Join me for this thread to learn more!

#python_builtins_by_driscollis

๐Ÿงต๐Ÿ๐Ÿ‘‡
#Python's `open()` function actually has quite a few different parameters that you can use:
The most commonly used parameters you will use with #Python's `open()` function are:

๐Ÿ file - The path to the file
๐Ÿ mode - Read, write, binary, etc
๐Ÿ encoding - What encoding the file is in
Read 8 tweets
Feb 16
We have already learned about the `bin()` and `hex()` functions. Today you will learn about #Python's handy `oct()` function!

`oct()` is used to convert an integer number to an octal string prefixed with โ€œ0oโ€

#python_builtins_by_driscollis

๐Ÿงต๐Ÿ๐Ÿ‘‡
Here are a couple of examples of using the `oct()` function with positive and negative numbers:
The #Python documentation has a nice example that shows how to format octal numbers using string formatting:
Read 4 tweets
Feb 15
Blockchain is a super popular topic here on Twitter. While I am not a blockchain developer, I thought it would be fun to put together a short thread on blockchain and #Python

Check it out if you're interested in this topic!

๐Ÿงต๐Ÿ๐Ÿ‘‡
ActiveState is a well-respected #Python company with lots of great content and they have an intro article on blockchain with Python:

activestate.com/blog/how-to-buโ€ฆ @ActiveState
@ActiveState The GeekFlare website also has a popular article on creating a blockchain with #Python:

geekflare.com/create-a-blockโ€ฆ
Read 7 tweets
Feb 15
Python includes a built-in named `object`. This built-in isn't a function. It's actually a class.

The `object` class is the base class of all Python classes. It has methods that are common to all instances of Python classes.

#python_builtins_by_driscollis

๐Ÿงต๐Ÿ๐Ÿ‘‡
The following two examples show equivalent empty classes in Python.

They both subclass `object`: Image
You can verify that a class is a subclass of a specific class using Python's built-in `issubclass()`. Image
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(