Python Forum
python create function validation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python create function validation
#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


Messages In This Thread
python create function validation - by mg24 - Nov-15-2022, 12:22 AM
RE: python create function validation - by deanhystad - Nov-15-2022, 01:57 AM

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,490 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  create my exception to my function korenron 2 819 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create a function for writing to SQL data to csv mg24 4 1,219 Oct-01-2022, 04:30 AM
Last Post: mg24
  Create SQL connection function and validate mg24 1 975 Sep-30-2022, 07:45 PM
Last Post: deanhystad
  How to define a function to create a resorted list? sparkt 6 2,872 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,508 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,758 Jun-21-2020, 09:40 PM
Last Post: jefsummers
  create function let_to_num() al_Czervik 2 2,145 Apr-17-2020, 10:44 PM
Last Post: al_Czervik
  Create function around sql ayomayam 1 1,618 Feb-05-2020, 07:20 PM
Last Post: ayomayam
  Create a function to find words of certain length ag4g 2 4,118 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