Skip to content

Commit

Permalink
parse echo args in argparser instead of cat
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Mar 11, 2024
1 parent 1369e86 commit f642020
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
15 changes: 2 additions & 13 deletions cat_win/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _show_version() -> None:


def _show_debug(args: list, unknown_args: list, known_files: list, unknown_files: list,
echo_args: list, valid_urls: list) -> None:
echo_args: str, valid_urls: list) -> None:
"""
Print all neccassary debug information
"""
Expand All @@ -219,7 +219,7 @@ def _show_debug(args: list, unknown_args: list, known_files: list, unknown_files
err_print('unknown_files: ', end='')
err_print(unknown_files)
err_print('echo_args: ', end='')
err_print(echo_args)
err_print(repr(echo_args))
err_print('valid_urls: ', end='')
err_print(valid_urls)
err_print('file encoding: ', end='')
Expand Down Expand Up @@ -1115,17 +1115,6 @@ def init(shell: bool = False) -> tuple:
config.save_config()
sys.exit(0)

if holder.args_id[ARGS_ECHO]:
echo_args = ' '.join(echo_args)
for arg, param in holder.args:
if arg == ARGS_ECHO:
if param.isupper():
try:
echo_args = echo_args.encode(arg_parser.file_encoding).decode('unicode_escape').encode('latin-1').decode(arg_parser.file_encoding)
except UnicodeError:
pass
break

Editor.set_indentation(const_dic[DKW.EDITOR_INDENTATION], const_dic[DKW.EDITOR_AUTO_INDENT])
Editor.set_flags(holder.args_id[ARGS_STDIN] and on_windows_os,
holder.args_id[ARGS_DEBUG], arg_parser.file_encoding)
Expand Down
16 changes: 14 additions & 2 deletions cat_win/tests/test_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,26 @@ def test_get_arguments_unknown_args(self):
def test_get_arguments_echo_args(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-E', '-n', 'random', test_text_file_dir])
['CAT', '-n', '--echo', '-n', 'random', test_text_file_dir])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '--echo'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertEqual(echo_args, '-n random ' + test_text_file_dir)

def test_get_arguments_echo_args_escaped(self):
arg_parser = ArgParser()
args, unknown_args, unknown_files, echo_args = arg_parser.get_arguments(
['CAT', '-n', '-E', '-n', '\\n', 'random'])
known_files = arg_parser.get_files()
args = list(map(lambda x: x[1], args))
self.assertCountEqual(args, ['-n', '-E'])
self.assertCountEqual(unknown_args, [])
self.assertCountEqual(known_files, [])
self.assertCountEqual(unknown_files, [])
self.assertCountEqual(echo_args, ['-n', 'random', test_text_file_dir])
self.assertEqual(echo_args, '-n \n random')

def test_get_arguments_echo_args_recursive(self):
arg_parser = ArgParser()
Expand Down
11 changes: 9 additions & 2 deletions cat_win/util/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ArgParser:

def __init__(self, default_file_encoding: str = 'utf-8') -> None:
self.default_file_encoding: str = default_file_encoding
self.file_encoding: str = ''
self.unicode_echo: bool = False
self._clear_values()
self.reset_values()

Expand Down Expand Up @@ -169,7 +169,13 @@ def get_arguments(self, argv: list, delete: bool = False) -> tuple:
contains the paramater in a sorted manner
"""
self.gen_arguments(argv, delete)
return (self._args, self._unknown_args, self._unknown_files, self._echo_args)
echo_args = ' '.join(self._echo_args)
if self.unicode_echo:
try:
echo_args = echo_args.encode(self.file_encoding).decode('unicode_escape').encode('latin-1').decode(self.file_encoding)
except UnicodeError:
pass
return (self._args, self._unknown_args, self._unknown_files, echo_args)

def get_files(self, dot_files: bool = False) -> list:
"""
Expand Down Expand Up @@ -270,6 +276,7 @@ def _add_argument(self, param: str, delete: bool = False) -> bool:
if param in (arg.short_form, arg.long_form):
self._args.append((arg.arg_id, param))
if arg.arg_id == ARGS_ECHO:
self.unicode_echo = param.isupper()
return True
return False
possible_path = os.path.realpath(param)
Expand Down

0 comments on commit f642020

Please sign in to comment.