Lets just focus on what the parser does
parser = argparse.ArgumentParser(description='Shortens URLs with dft.ba')
parser.add_argument('LongURL',help='Custom string at the end of the shortened URL')
parser.add_argument('--source','-s',help='Custom string at the end of the shortened URL')
parser.add_argument('-c','--copy', action="store_true", default=False, help='Copy the shortened URL to the clipboard')
parser.add_argument('-q','--quiet', action="store_true", default=False, help="Execute without printing")
args = parser.parse_args()
print args # add to debug the `argparse` behavior
LongURL
is a positional argument that is always required. If missing you'll get the 'too few arguments' error message.
source
is optional, but when provided must include an argument. If not given args.source is None
. As written the source
argument must be given in ADDITION to the LongURL
one.
Both args.copy
and args.quiet
are booleans; default value is False
; and True
if given. (the default=False
parameter is not needed.)
I haven't tried to work through the logic using copy
and quiet
. That won't come into play if there are problems earlier with LongURL
and source
.
Compare these samples:
In [38]: parser.parse_args('one'.split())
Out[38]: Namespace(LongURL='one', copy=False, quiet=False, source=None)
In [41]: parser.parse_args('-s one two -c -q'.split())
Out[41]: Namespace(LongURL='two', copy=True, quiet=True, source='one')
It may also help to look at what parse_args
is parsing: sys.argv[1:]
(if you have doubts about what you are getting from .profile).