Day #85 - Creating command line utility in python

Day #85 - Creating command line utility in python

Β·

3 min read

Introduction

Welcome to my 85th blog post on the Python journey. On day 85, I learned about creating a command line utility in Python. These are the programs that can be run from the command line window or the terminal. Let's dive into more details and understand how to create a command line utility.

So let's get started......

Creating Command Line Utility

  • These are the programs that can be run on the command line interface or the Windows terminal.

  • They help to access the functions directly through the command line.

  • In Python, we create our own command line utility using the built-in 'argparse' module

Syntax -

Below is the syntax for creating a command line utility using 'argparse' in Python:

import argparse

parser = argparse.ArgumentParser()

# Add command line arguments
parser.add_argument("arg1", help="description of argument 1")
parser.add_argument("arg2", help="description of argument 2")

# Parse the arguments
args = parser.parse_args()

# Use the arguments in your code
print(args.arg1)
print(args.arg2)

Example -

Adding optional arguments

Below is an example, that shows how to add an optional argument to command line utility:

import argparse

parser = argparse.ArgumentParser()

#adding optional arguments to command line
parser.add_argument("-o", "--optional", help="description of optional argument", default="default_value") 

args = parser.parse_args()

print(args.optional)

Adding positional arguments

The following example shows how to add a positional argument to command line utility:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("positional", help="description of positional argument")

args = parser.parse_args()

print(args.positional)

Adding arguments with type

The following example shows how to add an argument with a specified type:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("-n", type=int, help="description of integer argument")

args = parser.parse_args()

print(args.n)

Resources Used

You can watch the video of Day#85 by clicking on the below link πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 85, I learned about creating the command line utility that can be run using the command line or Windows terminal. I also learned about different ways to create command line utilities depending on the arguments that we pass.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #85

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


Β