Python Forum
python create function validation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python create function validation
#1
Hi Team,

how to create separate function for validating command line arguments in another module
and import the same in main module


def main():
    print("Directory watcher application")

    if (len(argv) < 2):
        print("Insufficient arguments")
        exit()

    if (argv[1] == "-h"):
        print("This script will travel the directory and display the names of all entries")
        exit()

    if (argv[1] == "-u"):
        print("Usage : Application_name Direcory_Name")
        exit()

    Directory_Watcher(argv[1])


if (__name__ == "__main__"):
    main()
Reply
#2
Command line parsing and verification is hard. Maybe not in this case, but quite often it can be hard. Use argparse if the command line is fairly simple. Use Click for a complicated command line interface. Both provide verification and even help (or --help).

A stab at using argparse to parse the 1 positional argument.
import argparse

def directory_watcher(directory):
    print("Watching", directory)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        prog="dir_watcher",
        description="Travel the directory and display names of all entries")
    parser.add_argument("directory", help="Directory to watch")
    args = parser.parse_args()
    directory_watcher(arg.directory)
argparse automatically creates -h --help
If you really want usage, that could be an optional argument. How is -u different from -h?
Output:
> python dir_watcher.py -h usage: dir_watcher [-h] directory Travel the directory and display names of all entries positional arguments: directory Directory to watch optional arguments: -h, --help show this help message and exit > > > python dir_watcher.py stuff Watching stuff
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,456 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  create my exception to my function korenron 2 798 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,179 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 958 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  How to define a function to create a resorted list? sparkt 6 2,848 Aug-08-2020, 04:10 PM
Last Post: sparkt
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,444 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Tried to create a function in a Datacamp course - why a is not equal to x_copy? danlin123 1 1,744 Jun-21-2020, 09:40 PM
Last Post: jefsummers
  create function let_to_num() al_Czervik 2 2,130 Apr-17-2020, 10:44 PM
Last Post: al_Czervik
  Create function around sql ayomayam 1 1,599 Feb-05-2020, 07:20 PM
Last Post: ayomayam
  Create a function to find words of certain length ag4g 2 4,095 Apr-21-2019, 06:20 PM
Last Post: BillMcEnaney

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020