1

I've just created a Python project that uses argparse for parsing arguments. But it seems that it does not support multi-line arguments. One can check the example/command-line.sh in the project, and will find it does not understand the following style

#!/bin/bash
../scripts_gen.py --template template.txt \
                  --save-to scripts \
                  --param "{'data':'datasets.txt', \
                            'lambda':[`echo 0.{0..9}|sed -E 's/\s+/,/g'`], \
                            'seed':[233,874]}" \
                  --format "{data}_lambda={lambda}_seed={seed}.sh" \
                  --delete

Note that this is legal in shell script: one can always write

$ ls -l \
> -f

in console or shell script file (no prompt in this case). So can I add support for this via argparse? Thank you.

7
  • This should be OK. What error do you see? You might not need the `` within your quoted string. Commented Jun 7, 2013 at 20:13
  • @SethMMorton In the above example, I will see the Python script can recognize the first argument --template but ignoring the rest. Then the shell later complains that it cannot find commands like --save-to etc.. Commented Jun 7, 2013 at 20:15
  • 2
    The problem is in your bash script. If it worked as intended, python wouldn't even know there's a newline between arguments--the shell handles that and removes the newline. Most likely there's a space after the first backslash. Commented Jun 7, 2013 at 22:30
  • @alexis I was using Cygwin but later tried Debian 7.0 and it seems OK. It might be a problem of Cygwin. But Cygwin can recognize the ls example, so it's still a little weird. Commented Jun 8, 2013 at 9:42
  • Ok, if the same file works properly on Debian, then it's not a space after the backslash. Perhaps it's a problem with the line endings in your script? Commented Jun 8, 2013 at 17:17

1 Answer 1

1

Post-facto answer, based on my comments:

  1. The problem is in your bash script. If it worked as intended, python wouldn't even know there's a newline between arguments--the shell handles that and removes the newline. Most likely there's a space after the first backslash.

  2. But since the same file works properly on Debian but fails under cygwin, it was not a space after the backslash. Perhaps it's a problem with the line endings in your script?

  3. So, the problem was automatic CR/LF conversion by git, combined with a strange refusal by cygwin to understand the line-ending conventions of its host operating system. Though you fixed it by hand-converting the script back to unix line endings, I would recommend a more robust solution: You could enable CR/LF support in cygwin (since you imply that it is an option), but my preference would be to also disable git's CR/LF mapping. And check that all your common tools and editors are configured to handle both kinds of line ending properly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.