Skip to content

Commit

Permalink
fix: properly support reading from stdin (#307)
Browse files Browse the repository at this point in the history
* Adjust cli when reading from stdin.

Bind reading from stdin on specifying `-i -`. This is part of
[`argparse.FileType`](https://docs.python.org/3/library/argparse.html?highlight=pseudo-argument#argparse.FileType).

Local tests under the following conditions:

 * implicit reading `poetry.lock` using args `-p -o -`
 * explicit reading `poetry.lock` using args `-p -i poetry.lock -o -`
 * explicit reading `poetry.lock` file after renaming using
   `cat p.lock | python -m cyclonedx_py.client -p -i - -o -`

Signed-off-by: Theodor van Nahl <theo@van-nahl.org>
  • Loading branch information
tvannahl authored Feb 3, 2022
1 parent 4f87341 commit 23f31a0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Input Method:
Flags to determine how `cyclonedx-bom` obtains it's input
-i FILE_PATH, --in-file FILE_PATH
File to read input from, or STDIN if not specified
File to read input from. Use "-" to read from STDIN.
SBOM Output Configuration:
Choose the output format and schema version
Expand Down
5 changes: 3 additions & 2 deletions cyclonedx_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ def get_arg_parser() -> argparse.ArgumentParser:
)
input_method_group.add_argument(
'-i', '--in-file', action='store', metavar='FILE_PATH',
type=argparse.FileType('r'), default=(None if sys.stdin.isatty() else sys.stdin),
help='File to read input from, or STDIN if not specified', dest='input_source', required=False
type=argparse.FileType('r'), # FileType does handle '-'
default=None,
help='File to read input from. Use "-" to read from STDIN.', dest='input_source', required=False
)

output_group = arg_parser.add_argument_group(
Expand Down

0 comments on commit 23f31a0

Please sign in to comment.