question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

incorrect parsing

See original GitHub issue

here is a script I am trying to get working

#!/usr/bin/env python

"""Send emails.

Usage:
    sendmail.py -s <subject> --to <to-addr> [ --from <from-addr> ] [ --cc <cc-addr> ] [ --bcc <bcc-addr> ]
    sendmail.py (-h | --help)
Options:
    -h show this help
"""

from docopt import docopt
from sys import argv, stdin

if __name__ == '__main__':
  arguments = docopt(__doc__)
 
if arguments['--from'] == False:
  fromaddr = "barney@example.com"
else:
  fromaddr = arguments['<from-addr>']
  print("you'll need to edit the script to add the password for this email.")
  exit(1)

body = stdin.read()


subject = arguments['<subject>']
toaddrs = arguments['<to-addr>']
print(arguments)

When I invoke the script like so:

echo foobar | ./sendmail.py -s blah --to blah@blah.blah --cc other@some.email --bcc hidden@some.email

I get this output:

{'--bcc': True, '--cc': True, '--from': False, '--help': False, '--to': True, '-h': [], '-s': True, '<bcc-addr>': None, '<cc-addr>': 'hidden@some.email', '<from-addr>': 'other@some.email', '<subject>': 'blah', '<to-addr>': 'blah@blah.blah', 'Options:': False, 'help': False, 'this': False}

But that is plainly incorrect: from-addr was never specified, so it should be “barney@example.com”, while bcc-addr should be what cc-addr is, and cc-addr should be what from-addr is. Some kind of weird argument shifting is happening, and it seems like the argument keys are being completely ignored.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
PertuyFcommented, Aug 3, 2017

It seems that if <argument> is mentioned in the Options section

Usage:
  script.py -o <argument>

Options:
  -o --option <argument>  Description.

And you call it with script.py -o my_arg, then docopt will return:

{
  "--option": "my_arg"
}

While if you do not mention <argument> in the Options section it will return:

{
  "--option": true,
  "<argument>": "my_arg"
}
1reaction
SeeSpotRuncommented, Jul 22, 2017

You need to include all the options in the Options section; also need a blank line between Usage and Options. Try:

Usage:
    sendmail.py [-s <subject>] [--to <to-addr>] [--from <from-addr>] [--cc <cc-addr>] [--bcc <bcc-addr>]
    sendmail.py (-h | --help)

Options:
    -s <subject>           Required subject
    --to <to-addr>         Required addressee
    --from <from-addr>     Sets from addr
    --cc <cc-addr>         Sets cc addr
    --bcc <bcc-addr>       Sets bcc addr
    -h show this help
"""
...
if arguments['--from'] is None:
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

The BIG-IP HTTP parser can incorrectly parse a tab character
Incorrect parsing occurs for the HTTP::uri iRule command, traffic policies, or BIG-IP ASM. Symptoms. As a result of this issue, you may ...
Read more >
CWE-1389: Incorrect Parsing of Numbers with Different Radices
If the developer assumes decimal-only inputs, the code could produce incorrect numbers when the inputs are parsed using a different base. This ...
Read more >
Loose parser incorrect parsing with incoherent indentation #683
Acorn's loose parser incorrectly parses code if the indentation is not coherent. This problem does not occur with the main parser.
Read more >
Incorrect parsing of wdl - Terra Support
Hello Terra Team, I noticed strange behavior in the method configuration. It suddenly got populated with a large number of default...
Read more >
Incorrect parsing with /permissive- - Visual Studio Feedback
Compiling the following code with /permissive-: template<typename T, typename... Us> class table { template<T...> struct test {}; template<T value>
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found