Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Exposed more options to CLI (#319)
Browse files Browse the repository at this point in the history
* Exposed more options to CLI

* Fixed the implementation
  • Loading branch information
roll authored May 4, 2020
1 parent e900252 commit 35f70b1
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tabulator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import click
import tabulator
from . import config
from . import exceptions


# Module API
Expand All @@ -19,6 +20,15 @@
@click.option('--format')
@click.option('--encoding')
@click.option('--limit', type=click.INT)
@click.option('--sheet')
@click.option('--fill-merged-cells', is_flag=True, default=None)
@click.option('--preserve-formatting', is_flag=True, default=None)
@click.option('--adjust-floating-point-error', is_flag=True, default=None)
@click.option('--table')
@click.option('--order_by')
@click.option('--resource')
@click.option('--property')
@click.option('--keyed', is_flag=True, default=None)
@click.version_option(config.VERSION, message='%(version)s')
def cli(source, limit, **options):
"""Command-line interface
Expand All @@ -32,19 +42,41 @@ def cli(source, limit, **options):
--format TEXT
--encoding TEXT
--limit INTEGER
--sheet TEXT/INTEGER (excel)
--fill-merged-cells BOOLEAN (excel)
--preserve-formatting BOOLEAN (excel)
--adjust-floating-point-error BOOLEAN (excel)
--table TEXT (sql)
--order_by TEXT (sql)
--resource TEXT/INTEGER (datapackage)
--property TEXT (json)
--keyed BOOLEAN (json)
--version Show the version and exit.
--help Show this message and exit.
```
"""

# Normalize options
options = {key: value for key, value in options.items() if value is not None}
with tabulator.Stream(source, **options) as stream:
cast = str
if six.PY2:
cast = unicode # noqa
if stream.headers:
click.echo(click.style(', '.join(map(cast, stream.headers)), bold=True))
for count, row in enumerate(stream, start=1):
click.echo(','.join(map(cast, row)))
if count == limit:
break
try:
options['sheet'] = int(options.get('sheet'))
options['resource'] = int(options.get('resource'))
except Exception:
pass

# Read the table
try:
with tabulator.Stream(source, **options) as stream:
cast = str
if six.PY2:
cast = unicode # noqa
if stream.headers:
click.echo(click.style(', '.join(map(cast, stream.headers)), bold=True))
for count, row in enumerate(stream, start=1):
click.echo(','.join(map(cast, row)))
if count == limit:
break
except exceptions.TabulatorException as exception:
click.echo('[error] %s' % str(exception))
exit(1)

0 comments on commit 35f70b1

Please sign in to comment.