Skip to content

Commit d3f1f5c

Browse files
committed
fix(cli): make the input, output and auto-extension help match the code (#1796)
The help for -o, the positional input and -a/--auto-extension described extensions as always appended, but main() forwards -a as Extractor(extension=...), off by default, and make_name() applies it to both names: ".pcap" to the input unless it already ends in a PCAP_EXT suffix, and the format extension to the output unless -F makes it a directory. -o also read "The name of input pcap file". - pcapkit/__main__.py: the help strings now state that behaviour. - docs/source/pcapkit/index.rst: the CLI block regenerated from `python -m pcapkit --help` at COLUMNS=80. - tests/cli/test_main.py: pin each corrected help; every new assertion fails on origin/main. Closes #1796
1 parent 925c84e commit d3f1f5c

3 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎docs/source/pcapkit/index.rst‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@ Command Line Tool
8282
PCAP file extractor and formatted dumper
8383
8484
positional arguments:
85-
input-file-name The name of input pcap file. If ".pcap" omits, it will
86-
be automatically appended. Use "-" to indicate reading
87-
from `stdin'.
85+
input-file-name The name of input pcap file. With `--auto-extension`,
86+
".pcap" is appended unless its extension is already
87+
".pcap", ".cap" or ".pcapng". Use "-" to indicate
88+
reading from `stdin'.
8889
8990
options:
9091
-h, --help show this help message and exit
9192
-V, --version show program's version number and exit
9293
-o, --output file-name
93-
The name of input pcap file. If format extension
94-
omits, it will be automatically appended.
94+
The name of output report file. If this omits, "out"
95+
is used. With `--auto-extension`, the extension of the
96+
output format (e.g. ".json") is appended if absent.
97+
With `--files`, the directory to store one report file
98+
per frame.
9599
-f, --format format Print a extraction report in the specified output
96100
format. Available are all formats supported by
97101
dictdumper, e.g.: json, plist, and tree.
@@ -105,7 +109,10 @@ Command Line Tool
105109
-t, --tree Display extraction report as tree view text. This will
106110
yield "raw" output that may be used by external tools.
107111
This option overrides all other options.
108-
-a, --auto-extension If output file extension omits, append automatically.
112+
-a, --auto-extension Append missing file extensions: ".pcap" to the input
113+
file name, and the extension of the output format
114+
(e.g. ".json") to the output file name unless
115+
`--files` is set.
109116
-v, --verbose Show more information.
110117
-F, --files Split each frame into different files.
111118
-E, --engine PKG Indicate extraction engine. Note that except default

‎pcapkit/__main__.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ def get_parser() -> 'ArgumentParser':
4646
description='PCAP file extractor and formatted dumper')
4747
parser.add_argument('-V', '--version', action='version', version=__version__)
4848
parser.add_argument('fin', metavar='input-file-name',
49-
help=('The name of input pcap file. If ".pcap" omits, '
50-
'it will be automatically appended. Use "-" to indicate '
49+
help=('The name of input pcap file. With `--auto-extension`, '
50+
'".pcap" is appended unless its extension is already '
51+
'".pcap", ".cap" or ".pcapng". Use "-" to indicate '
5152
'reading from `stdin\'.'))
5253
parser.add_argument('-o', '--output', action='store', metavar='file-name', dest='fout',
53-
help=('The name of input pcap file. If format extension '
54-
'omits, it will be automatically appended.'))
54+
help=('The name of output report file. If this omits, "out" '
55+
'is used. With `--auto-extension`, the extension of the '
56+
'output format (e.g. ".json") is appended if absent. '
57+
'With `--files`, the directory to store one report file '
58+
'per frame.'))
5559
parser.add_argument('-f', '--format', action='store', metavar='format', dest='format',
5660
help=('Print a extraction report in the specified output '
5761
'format. Available are all formats supported by '
@@ -70,7 +74,10 @@ def get_parser() -> 'ArgumentParser':
7074
'will yield "raw" output that may be used by external '
7175
'tools. This option overrides all other options.'))
7276
parser.add_argument('-a', '--auto-extension', action='store_true', default=False,
73-
help='If output file extension omits, append automatically.')
77+
help=('Append missing file extensions: ".pcap" to the input '
78+
'file name, and the extension of the output format '
79+
'(e.g. ".json") to the output file name unless `--files` '
80+
'is set.'))
7481
parser.add_argument('-v', '--verbose', action='store_true', default=False,
7582
help='Show more information.')
7683
parser.add_argument('-F', '--files', action='store_true', default=False,

‎tests/cli/test_main.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,79 @@ def test_get_parser_parses_expected_arguments(self) -> None:
211211
self.assertTrue(args.json)
212212
self.assertTrue(args.files)
213213

214+
def test_output_help_describes_the_output_file(self) -> None:
215+
"""``-o``'s help names the output report, not the input capture.
216+
217+
Issue #1796: the help was copied from the positional ``input-file-name``
218+
and read "The name of input pcap file", so ``--help`` said ``-o`` named
219+
the file being *read*. It also said the extension is always appended,
220+
but :func:`~pcapkit.__main__.main` forwards ``-a``/``--auto-extension``
221+
as ``Extractor(extension=...)``, which is off by default.
222+
223+
"""
224+
emoji = types.SimpleNamespace(emojize=lambda text: text)
225+
module, _, _ = self._load_cli_module(emoji_module=emoji)
226+
227+
parser = module.get_parser()
228+
action = next(action for action in parser._actions # pylint: disable=protected-access
229+
if action.dest == 'fout')
230+
self.assertEqual(action.option_strings, ['-o', '--output'])
231+
232+
text = action.help.lower()
233+
self.assertIn('output', text)
234+
self.assertNotIn('input', text)
235+
# ``Extractor.__init__`` substitutes "out" for an omitted ``fout``.
236+
self.assertIn('"out"', text)
237+
# The extension depends on ``-a``, and ``-F`` makes ``fout`` a directory.
238+
self.assertIn('--auto-extension', text)
239+
self.assertIn('--files', text)
240+
241+
def test_input_help_ties_the_pcap_extension_to_auto_extension(self) -> None:
242+
"""``input-file-name``'s help appends ``.pcap`` only under ``-a``.
243+
244+
Issue #1796: the help said ".pcap" is always appended when omitted, but
245+
:meth:`~pcapkit.foundation.extraction.Extractor.make_name` appends it
246+
only with ``extension=True`` -- ``-a`` -- and then unless the name
247+
already ends in one of ``Extractor.PCAP_EXT``. Without ``-a``,
248+
``pcapkit-cli in`` fails with ``FileNotFound`` for ``in``.
249+
250+
"""
251+
emoji = types.SimpleNamespace(emojize=lambda text: text)
252+
module, _, _ = self._load_cli_module(emoji_module=emoji)
253+
254+
parser = module.get_parser()
255+
action = next(action for action in parser._actions # pylint: disable=protected-access
256+
if action.dest == 'fin')
257+
258+
text = action.help.lower()
259+
self.assertIn('--auto-extension', text)
260+
self.assertNotIn('automatically appended', text)
261+
# ``Extractor.PCAP_EXT``: a name already ending in one is left alone.
262+
self.assertIn('".cap"', text)
263+
self.assertIn('".pcapng"', text)
264+
265+
def test_auto_extension_help_covers_input_and_output(self) -> None:
266+
"""``-a``'s help names the input extension as well as the output one.
267+
268+
Issue #1796: the help mentioned only the output file, but ``-a`` is
269+
forwarded as ``Extractor(extension=...)``, which
270+
:meth:`~pcapkit.foundation.extraction.Extractor.make_name` applies to
271+
the input name too -- and not to ``fout`` under ``-F``, where it is a
272+
directory.
273+
274+
"""
275+
emoji = types.SimpleNamespace(emojize=lambda text: text)
276+
module, _, _ = self._load_cli_module(emoji_module=emoji)
277+
278+
parser = module.get_parser()
279+
action = next(action for action in parser._actions # pylint: disable=protected-access
280+
if action.dest == 'auto_extension')
281+
282+
text = action.help.lower()
283+
self.assertIn('input', text)
284+
self.assertIn('".pcap"', text)
285+
self.assertIn('--files', text)
286+
214287
def test_layer_and_protocol_reach_the_extractor(self) -> None:
215288
"""``-L``/``-P`` are forwarded under the names ``Extractor`` reads.
216289

0 commit comments

Comments
 (0)