Skip to main content
added 15 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to something like:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow interactive testing of this function.

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow interactive testing of this function.

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to something like:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow interactive testing of this function.

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments
deleted 416 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow interactive testing of this function:.

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow testing of this function:

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow interactive testing of this function.

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments
deleted 416 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": hashlib.md5None, "sha1": hashlib.sha1None, ..."sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('-f', '--filepath''filepath',
                        help="Path to file to run sums on.", 
                        required=True)
    parser.add_argument('hashes''--hashes',
                        nargs='*',
                        choices=digests,
                        default=['md5'default=["md5", 'sha1']"sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args) 

print _get_arguments()

The main difference now is that filepath now needs either the flag -f or --file in front of it. Alternatively it would have to come before the hashes, so it is possible to distinguish the possibly multiple hash arguments. The hashes don't take a - in front anymore either.

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow testing of this function:

>>>$ _get_arguments(['-f',python 'file'])argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

>>>$ _get_arguments(['-f',python 'file',argparse_new.py 'md5'])file --hashes md5
Namespace(filepath='file', hashes='md5')

>>>$ _get_arguments(['md5',python 'argparse_new.py file -f',-hashes 'file'])md5 sha256
Namespace(filepath='file', hashes=['md5']hashes=['md5', 'sha256'])

>>>$ _get_arguments(['python argparse_new.py -f',-hashes 'file',md5 'md5',sha256
usage: 'sha256',argparse_new.py 'sha512'])[-h]
Namespace(filepath='file', hashes=['md5'                      [--hashes [{sha256,sha1,md5} 'sha256'[{sha256,sha1,md5} 'sha512'])...]]]
                       filepath
argparse_new.py: error: too few arguments

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

digests = { "md5": hashlib.md5, "sha1": hashlib.sha1, ... }

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('-f', '--filepath',
                        help="Path to file to run sums on.", 
                        required=True)
    parser.add_argument('hashes',
                        nargs='*',
                        choices=digests,
                        default=['md5', 'sha1'],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args)

The main difference now is that filepath now needs either the flag -f or --file in front of it. Alternatively it would have to come before the hashes, so it is possible to distinguish the possibly multiple hash arguments. The hashes don't take a - in front anymore either.

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow testing of this function:

>>> _get_arguments(['-f', 'file'])
Namespace(filepath='file', hashes=['md5', 'sha1'])

>>> _get_arguments(['-f', 'file', 'md5'])
Namespace(filepath='file', hashes='md5')

>>> _get_arguments(['md5', '-f', 'file'])
Namespace(filepath='file', hashes=['md5'])

>>> _get_arguments(['-f', 'file', 'md5', 'sha256', 'sha512'])
Namespace(filepath='file', hashes=['md5', 'sha256', 'sha512'])

In addition to the comments from @vnp, which I agree with, consider simplifying your argparse to:

import argparse

digests = { "md5": None, "sha1": None, "sha256": None}

def _get_arguments(args=None):
    # Argument Parser for the overall function
    parser = argparse.ArgumentParser(
        description="Run hash sum functions on a file and return them.")
    parser.add_argument('filepath',
                        help="Path to file to run sums on.")
    parser.add_argument('--hashes',
                        nargs='*',
                        choices=digests,
                        default=["md5", "sha1"],
                        help="Hashes to be used. Default: md5, sha1")
    return parser.parse_args(args) 

print _get_arguments()

The choices of the hashes can use the dictionary of defined hashes @vnp recommended as well (No need for digests.keys() here, argparse does it correctly even when given a dictionary instead of a list).

I added the args parameter to allow testing of this function:

$ python argparse_new.py file
Namespace(filepath='file', hashes=['md5', 'sha1'])

$ python argparse_new.py file --hashes md5
Namespace(filepath='file', hashes='md5')

$ python argparse_new.py file --hashes md5 sha256
Namespace(filepath='file', hashes=['md5', 'sha256'])

$ python argparse_new.py --hashes md5 sha256
usage: argparse_new.py [-h]
                       [--hashes [{sha256,sha1,md5} [{sha256,sha1,md5} ...]]]
                       filepath
argparse_new.py: error: too few arguments
added 97 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 266 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading