Skip to content

Report a delimiter that is not a single character as a usage error - #1396

Open
qianeric-backup wants to merge 1 commit into
wireservice:masterfrom
qianeric-backup:fix/validate-single-character-options
Open

qianeric-backup wants to merge 1 commit into
wireservice:masterfrom
qianeric-backup:fix/validate-single-character-options

Conversation

@qianeric-backup

Copy link
Copy Markdown

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:

$ csvjson -q xx examples/dummy.csv
csvjson: error: --quotechar must be a single character, not 'xx'
$ csvformat -D '||' examples/dummy.csv
csvformat: error: --out-delimiter must be a single character, not '||'
case before after
`csvjson -d ' '`
csvjson -q xx, -p xx TypeError traceback usage error, exit 2
`csvformat -D ' '`
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. I left --snifflimit, --skipinitialspace and the numeric options alone.

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.

`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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant