Skip to content

Commit 4220273

Browse files
authored
Merge pull request #188 from andylolz/30-formats-cli
Stipulate possible formats in CLI parser
2 parents 8284d1e + 098f952 commit 4220273

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

examples/help/create-template/expected.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: flatten-tool create-template [-h] -s SCHEMA [-f OUTPUT_FORMAT]
1+
usage: flatten-tool create-template [-h] -s SCHEMA [-f {csv,xlsx,all}]
22
[-m MAIN_SHEET_NAME] [-o OUTPUT_NAME]
33
[--rollup] [-r ROOT_ID] [--use-titles]
44

@@ -7,7 +7,7 @@ optional arguments:
77
-s SCHEMA, --schema SCHEMA
88
Path to the schema file you want to use to create the
99
template
10-
-f OUTPUT_FORMAT, --output-format OUTPUT_FORMAT
10+
-f {csv,xlsx,all}, --output-format {csv,xlsx,all}
1111
Type of template you want to create. Defaults to all
1212
available options
1313
-m MAIN_SHEET_NAME, --main-sheet-name MAIN_SHEET_NAME

examples/help/flatten/expected.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: flatten-tool flatten [-h] [-s SCHEMA] [-f OUTPUT_FORMAT] [--xml]
1+
usage: flatten-tool flatten [-h] [-s SCHEMA] [-f {csv,xlsx,all}] [--xml]
22
[--id-name ID_NAME] [-m MAIN_SHEET_NAME]
33
[-o OUTPUT_NAME] [--root-list-path ROOT_LIST_PATH]
44
[--rollup] [-r ROOT_ID] [--use-titles]
@@ -11,7 +11,7 @@ optional arguments:
1111
-h, --help show this help message and exit
1212
-s SCHEMA, --schema SCHEMA
1313
Path to a relevant schema.
14-
-f OUTPUT_FORMAT, --output-format OUTPUT_FORMAT
14+
-f {csv,xlsx,all}, --output-format {csv,xlsx,all}
1515
Type of template you want to create. Defaults to all
1616
available options
1717
--xml Use XML as the input format

examples/help/unflatten/expected.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usage: flatten-tool unflatten [-h] -f INPUT_FORMAT [--xml] [--id-name ID_NAME]
1+
usage: flatten-tool unflatten [-h] -f {csv,xlsx} [--xml] [--id-name ID_NAME]
22
[-b BASE_JSON] [-m ROOT_LIST_PATH] [-e ENCODING]
33
[-o OUTPUT_NAME] [-c CELL_SOURCE_MAP]
44
[-a HEADING_SOURCE_MAP]
@@ -17,7 +17,7 @@ positional arguments:
1717

1818
optional arguments:
1919
-h, --help show this help message and exit
20-
-f INPUT_FORMAT, --input-format INPUT_FORMAT
20+
-f {csv,xlsx}, --input-format {csv,xlsx}
2121
File format of input file or directory.
2222
--xml Use XML as the output format
2323
--id-name ID_NAME String to use for the identifier key, defaults to 'id'

flattentool/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import print_function
22
import argparse
33
from flattentool import create_template, unflatten, flatten
4+
from flattentool.input import FORMATS as INPUT_FORMATS
5+
from flattentool.output import FORMATS as OUTPUT_FORMATS
46
from six import text_type
57

68
"""
@@ -28,6 +30,9 @@ def create_parser():
2830
parser = argparse.ArgumentParser()
2931
subparsers = parser.add_subparsers(dest='subparser_name')
3032

33+
output_formats = sorted(OUTPUT_FORMATS) + ['all']
34+
input_formats = sorted(INPUT_FORMATS)
35+
3136
parser_create_template = subparsers.add_parser(
3237
'create-template',
3338
help='Create a template from the given schema')
@@ -37,7 +42,8 @@ def create_parser():
3742
required=True)
3843
parser_create_template.add_argument(
3944
"-f", "--output-format",
40-
help="Type of template you want to create. Defaults to all available options")
45+
help="Type of template you want to create. Defaults to all available options",
46+
choices=output_formats)
4147
parser_create_template.add_argument(
4248
"-m", "--main-sheet-name",
4349
help="The name of the main sheet, as seen in the first tab of the spreadsheet for example. Defaults to main")
@@ -67,7 +73,8 @@ def create_parser():
6773
help="Path to a relevant schema.")
6874
parser_flatten.add_argument(
6975
"-f", "--output-format",
70-
help="Type of template you want to create. Defaults to all available options")
76+
help="Type of template you want to create. Defaults to all available options",
77+
choices=output_formats)
7178
parser_flatten.add_argument(
7279
"--xml",
7380
action='store_true',
@@ -105,6 +112,7 @@ def create_parser():
105112
parser_unflatten.add_argument(
106113
"-f", "--input-format",
107114
help="File format of input file or directory.",
115+
choices=input_formats,
108116
required=True)
109117
parser_unflatten.add_argument(
110118
"--xml",

0 commit comments

Comments
 (0)