Python – ArgParse
ArgParse
The argparse module in Python provides a simple and user-friendly framework for building command-line interfaces (CLIs). It allows developers to define expected arguments, automatically handles parsing from sys.argv, and generates helpful usage and help messages. Additionally, argparse reports errors when invalid or missing arguments are provided, improving the overall user experience.
Importing the Library
1
import argparse
Basic Usage
Defining the Parser
You can configure the script’s name, description, and concluding message using:
- prog – Sets the displayed name of your script or PoC.
- description – Provides a short explanation of what the script does.
- epilog – Displays additional text at the end of the help message.
For Example:-
1
2
3
parser = argparse.ArgumentParser(
prog='ScriptName',description='Short description of the script.',epilog='Thank you for using this tool!'
)
Adding Arguments
Use add_argument() to specify the expected inputs. For example, to define a required attacker IP parameter:
1
parser.add_argument('--ip', help="Attacker IP address", required=True)
Save it !
1
args = parser.parse_args()
Help
After defining all this above arguments.know you can run the --help or -h for help menu.
1
2
3
python3 file_name.py --help
python3 file_name.py -h
Resources
To explore the full capabilities of the argparse module, refer to the official documentation :- link
