Repository navigation
Report a delimiter that is not a single character as a usage error - #1396
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
`csvkit` hands `--delimiter`, `--quotechar`, `--escapechar` and
`--out-delimiter` straight to the `csv` module, so a wrong value ends the run
with a traceback instead of the usage error the same classes already produce
for other option mistakes:
$ csvjson -d '||' examples/dummy.csv
Traceback (most recent call last):
...
TypeError: "delimiter" must be a 1-character string
$ echo $?
1
$ csvformat -D '||' examples/dummy.csv
TypeError: "delimiter" must be a 1-character string
Other option problems in csvkit are reported by the parser:
$ csvjson --key value --stream examples/dummy.csv
usage: csvjson [-h] ...
csvjson: error: --key is only allowed with --stream when --lat and --lon are also specified.
## Change
$ csvjson -d '||' examples/dummy.csv ; echo $?
usage: csvjson [-h] ...
csvjson: error: --delimiter must be a single character, not '||'
2
The same check covers `--quotechar`, `--escapechar` and csvformat's
`--out-delimiter`.
| case | before | after |
|---|---|---|
| `csvjson -d '||'` | `TypeError` traceback, exit 1 | `csvjson: error: --delimiter must be a single character, not '||'`, exit 2 |
| `csvjson -q xx`, `-p xx` | `TypeError` traceback | same usage error |
| `csvformat -D '||'` | `TypeError` traceback | `csvformat: error: --out-delimiter must be a single character, not '||'`, exit 2 |
| valid single-character values | unchanged | unchanged |
Exit code 2 matches the other usage errors, so scripts that branch on the
exit code see a usage mistake instead of a crash.
## Tests
`test_single_character_options` in `tests/test_utilities/test_csvjson.py` and
`test_single_character_out_delimiter` in
`tests/test_utilities/test_csvformat.py`. `pytest tests/test_utilities`:
329 passed, 4 skipped with the change; the new tests fail on the current tree
with the `TypeError`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
csvkithands--delimiter,--quotechar,--escapecharand--out-delimiterstraight to thecsvmodule, so a wrong value ends the run with a traceback instead of the usage error the same classes already produce for other option mistakes:Other option problems in csvkit are reported by the parser:
Change
The same check covers
--quotechar,--escapecharand csvformat's--out-delimiter:csvjson -q xx,-p xxTypeErrortracebackExit code 2 matches the other usage errors, so scripts that branch on the exit code see a usage mistake instead of a crash. I left
--snifflimit,--skipinitialspaceand the numeric options alone.Tests
test_single_character_optionsintests/test_utilities/test_csvjson.pyandtest_single_character_out_delimiterintests/test_utilities/test_csvformat.py.pytest tests/test_utilities: 329 passed, 4 skipped with the change; the new tests fail on the current tree with theTypeError.