Skip to content

Instantly share code, notes, and snippets.

@mikecharles
Last active December 1, 2022 08:50
Show Gist options
  • Select an option

  • Save mikecharles/9ed3082b10d77d658743 to your computer and use it in GitHub Desktop.

Select an option

Save mikecharles/9ed3082b10d77d658743 to your computer and use it in GitHub Desktop.

Revisions

  1. mikecharles revised this gist Feb 8, 2016. 2 changed files with 8 additions and 8 deletions.
    8 changes: 4 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    Validates a `--dates` argument in Python with the following possible formats:

    - MMDD (eg. 0515)
    - MMDD-MMDD (eg. 0115-0315)
    - YYYYMMDD (eg. 20120515)
    - YYYYMMDD-YYYYMMDD (eg. 20140115-20140315)
    - yesterday
    - today

    @@ -35,8 +35,8 @@ def parse_args(argv):
    # Add arguments
    group.add_argument(
    '-d', '--dates', dest='dates',
    help='month and day of the valid date(s) to create stats for (MMDD or '
    'MMDD-MMDD)',
    help='month and day of the valid date(s) to create stats for (YYYYMMDD or '
    'YYYYMMDD-YYYYMMDD)',
    metavar='<DATE(S)>', required=True, type=date_arg)
    # If no options are set, print help and exit, otherwise parse args
    if len(argv) <= 1:
    8 changes: 4 additions & 4 deletions validate_date_arg.py
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ def date_arg(value):
    Function will ensure that the --dates argument matches a given regex.
    Possible values of the date are:
    - MMDD (eg. 0515)
    - MMDD-MMDD (eg. 0115-0315)
    - YYYYMMDD (eg. 20120515)
    - YYYYMMDD-YYYYMMDD (eg. 20140115-20140315)
    - yesterday
    - today
    @@ -27,12 +27,12 @@ def date_arg(value):
    regex
    """
    # Regex check
    if not re.match('^(\d{4}|\d{4}-\d{4}|yesterday|today)$', value):
    if not re.match('^(\d{8}|\d{8}-\d{8}|yesterday|today)$', value):
    raise argparse.ArgumentTypeError("must be in the form MMDD, "
    "MMDD-MMDD, yesterday, "
    "or today".format(value))
    # Make sure end date is >= start date
    if re.match("\d{4}-\d{4}", value):
    if re.match("\d{8}-\d{8}", value):
    start, end = value.split('-')
    if not end >= start:
    raise argparse.ArgumentTypeError("The start date is less than the "
  2. mikecharles revised this gist Feb 8, 2016. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -38,4 +38,16 @@ def parse_args(argv):
    help='month and day of the valid date(s) to create stats for (MMDD or '
    'MMDD-MMDD)',
    metavar='<DATE(S)>', required=True, type=date_arg)
    # If no options are set, print help and exit, otherwise parse args
    if len(argv) <= 1:
    parser.print_help()
    sys.exit()
    else:
    args = parser.parse_args(argv)
    # If the help option was set, print help and exit
    if hasattr(args, 'help'):
    parser.print_help()
    sys.exit()

    return args
    ```
  3. mikecharles revised this gist Feb 8, 2016. 1 changed file with 32 additions and 4 deletions.
    36 changes: 32 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,35 @@ Validates a `--dates` argument in Python with the following possible formats:

    To use this function, reference it in the `argparse` setup:

    group.add_argument('-d', '--dates', dest='dates',
    help='month and day of the valid date(s) to create stats for (MMDD or '
    'MMDD-MMDD)',
    metavar='<DATE(S)>', required=True, type=date_arg)
    ```python
    # ------------------------------------------------------------------------------
    # Function to parse command-line arguments
    #
    def parse_args(argv):
    """
    Parse the command line args
    Note that the argv argument will be parsed instead of sys.argv because
    the test suite will need to execute the main() function, and will
    therefore need to pass the given arguments to that function, as opposed
    to the main() function getting them from sys.argv.
    Parameters
    ----------
    - argv - *list of strings* - argument list containing the elements from
    sys.argv, except the first element (sys.argv[0]) which is the script name
    """
    # Create an ArgumentParser object
    parser = argparse.ArgumentParser(add_help=False, usage='%(prog)s [OPTIONS]')

    # Add a required argument group
    group = parser.add_argument_group('required arguments')

    # Add arguments
    group.add_argument(
    '-d', '--dates', dest='dates',
    help='month and day of the valid date(s) to create stats for (MMDD or '
    'MMDD-MMDD)',
    metavar='<DATE(S)>', required=True, type=date_arg)
    ```
  4. mikecharles created this gist Feb 8, 2016.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    Validates a `--dates` argument in Python with the following possible formats:

    - MMDD (eg. 0515)
    - MMDD-MMDD (eg. 0115-0315)
    - yesterday
    - today

    To use this function, reference it in the `argparse` setup:

    group.add_argument('-d', '--dates', dest='dates',
    help='month and day of the valid date(s) to create stats for (MMDD or '
    'MMDD-MMDD)',
    metavar='<DATE(S)>', required=True, type=date_arg)
    40 changes: 40 additions & 0 deletions validate_date_arg.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    def date_arg(value):
    """
    Validate the --dates argument
    Function will ensure that the --dates argument matches a given regex.
    Possible values of the date are:
    - MMDD (eg. 0515)
    - MMDD-MMDD (eg. 0115-0315)
    - yesterday
    - today
    Parameters
    ----------
    - value - *string* - automatically passed by the ArgumentParser object
    Returns
    -------
    Returns the value back to the ArgumentParser object
    Exceptions
    ----------
    - argparse.ArgumentTypeError - if the passed argument doesn't match the
    regex
    """
    # Regex check
    if not re.match('^(\d{4}|\d{4}-\d{4}|yesterday|today)$', value):
    raise argparse.ArgumentTypeError("must be in the form MMDD, "
    "MMDD-MMDD, yesterday, "
    "or today".format(value))
    # Make sure end date is >= start date
    if re.match("\d{4}-\d{4}", value):
    start, end = value.split('-')
    if not end >= start:
    raise argparse.ArgumentTypeError("The start date is less than the "
    "end date")
    return value