Skip to content

Commit

Permalink
Add --delimiter option to set CSV field separator (#21)
Browse files Browse the repository at this point in the history
First suggested in #14.
  • Loading branch information
flother authored Feb 9, 2025
1 parent 581eb1e commit 364c612
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version 0.3.0 (unreleased)

- Switch to using [uv](https://github.com/astral-sh/uv) for project management
- Add `--delimiter` command-line option. Use it to choose the character that separates CSV fields in the output

## Version 0.2.0 (3 Jan 2022)

Expand Down
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ By default, `NA`, `N/A`, `.`, and `-` are considered null values. These are not

The short form of this option is `-n`.

### `--delimiter`

Let's you choose the character used to separate fields in the CSV output. By default a comma (`,`) is used as the delimiter. If you want to use a tab as a delimiter, pass `--delimiter $'\t'` (Bash and Zsh) or `--delimiter (printf '\t')` (Fish).

### `--version`

Show the version of HTMLTab you have installed, and exit.
Expand Down
16 changes: 15 additions & 1 deletion src/htmltab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
"strings. Use multiple times if you have more than one "
"currency symbol [default: '{}']".format("', '".join(DEFAULT_CURRENCY_SYMBOLS)),
)
@click.option(
"--delimiter",
"-e",
type=str,
default=",",
show_default=True,
help="Character used to separate fields in the CSV output",
)
@click.option(
"--output",
"-o",
Expand All @@ -85,6 +93,7 @@ def main(
group_symbol: str,
decimal_symbol: str,
currency_symbol: list[str],
delimiter: str,
output: IO[Any],
html_file: Callable[[], str],
):
Expand Down Expand Up @@ -127,6 +136,11 @@ def main(
# Do nothing on platforms without signals or ``SIGPIPE``.
pass

# Check the CSV delimiter is a single character before requesting and
# parsing the HTML.
if len(delimiter) != 1:
raise click.UsageError("delimiter must be a single character")

# Parse file contents as HTML.
try:
doc = parse_html(html_file())
Expand Down Expand Up @@ -219,7 +233,7 @@ def main(
rows.append(row)

# Output the CSV to stdout.
out = csv.writer(output)
out = csv.writer(output, delimiter=delimiter)
for row in rows:
# Extra empty cells are added to the row as required, to ensure that
# all rows have the same number of fields (as required by the closest
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def basic_csv():
return file_contents


@pytest.fixture(scope="session")
def basic_tsv():
with open("tests/fixtures/basic.tsv") as fh:
file_contents = fh.read()
return file_contents


@pytest.fixture(scope="session")
def basic_european_csv():
with open("tests/fixtures/basic_european.csv") as fh:
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/basic.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Col A Col B
1 2
3 4
5 6
3.142 3142
3142 3.142
12 changes: 12 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ def test_keep_numbers(runner, three_csv_table_two_keep):
assert result2.output == three_csv_table_two_keep


def test_delimiter(runner, basic_tsv):
result = runner.invoke(main, ["-e", " ", "tests/fixtures/basic.html"])
assert result.exit_code == 0
assert result.output == basic_tsv


def test_delimiter_must_be_single_character(runner):
result = runner.invoke(main, ["-e", "foo", "tests/fixtures/basic.html"])
assert result.exit_code != 0
assert "Error: delimiter must be a single character" in result.output


def test_bad_input(runner):
result = runner.invoke(main, input="<")
assert result.exit_code != 0
Expand Down

0 comments on commit 364c612

Please sign in to comment.