Home > Mobile >  Need a way to ignore command line arguments if none are entered for python shell script
Need a way to ignore command line arguments if none are entered for python shell script

Time:01-23

Code is as follows. When I run the script with the two arguments specified (user and alert) the script runs perfectly. However, I need to find a way for the script not to say the following when I don't provide any arguments.

disk-monitor.py: error: the following arguments are required: user, alert

If there are no arguments provided, the user value should be set to "kkelly" and the alert value should be set to 90, and the script should execute with those values.

#!/usr/bin/env python3

import os
import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument("user", help="Which user would you like to send the alert to")
parser.add_argument("alert", help="What would you like to alert set to?")
args = parser.parse_args()

if len(sys.argv) >= 2:
    # The local email account to send notifications to
    admin = args.user
    # The percentage use at which notifications will be sent
    alert = int(args.alert)
else:
    admin = "kkelly"
    alert = 90

# Capture the machine's hostname
hostname = os.popen("hostname").read().strip()
# current timestamp
date = os.popen("date").read().strip()

# Gather %use and device/filesystem of each volume
diskusage = os.popen("df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 \" \" $1 }'")
# Loop through each volume entry
for disk in diskusage.readlines():
    # Extract percentage use
    usage = os.popen("echo "   disk.strip()   " | awk '{ print $1}' | cut -d'%' -f1").read().strip()
    # extract device/filesystem name
    partition = os.popen("echo "   disk.strip()   " | awk '{ print $2 }'")
    # Convert usage into intege
    usageint = int(usage)
    partitionstring = partition.read().strip()
    # if %use of this volume is above the point at which we want to be notified...
    if usageint > alert:
        # ...send that notification
        os.system(
            "echo '"   hostname   " is running out of space on "   partitionstring   " partition.  As of "   date   " it is at "   usage   "%.' | mail -s 'Alert: "   hostname   " is almost out of disk space' "   admin)

Tried importing the sys module to check for arguments but I don't really understand what I am doing.

CodePudding user response:

The reason it won't allow you set them to non-required even when they have a default set is because they're positional arguments.

If you instead make them options you can run your code with default values and a non-required flag as such:

parser.add_argument(
    "-u", "--user", help="Which user would you like to send the alert to", required=False, default="kkelly")
parser.add_argument(
    "-a", "--alert", help="What would you like to alert set to?", required=False, default="90")

This can be seen in the argparse docs here :)

CodePudding user response:

Providing a default value isn't enough. You also need to set nargs='?' to mark the arguments optional:

parser.add_argument("user", nargs='?', default='kkelly', help="Which user would you like to send the alert to")
parser.add_argument("alert", nargs='?', default='90', help="What would you like to alert set to?")
  • Related