Skip to content

Commit

Permalink
Merge pull request #9 from charterchap/add_command_line_option
Browse files Browse the repository at this point in the history
Adds more command line options
  • Loading branch information
eerimoq committed Jan 25, 2018
2 parents 36b6f8d + fbb0756 commit 2129433
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 21 deletions.
45 changes: 45 additions & 0 deletions bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,23 @@ def _do_info(args):
f.add_file(binfile)
print(f.info())

def _do_as_hexdump(args):
for binfile in args.binfile:
f = BinFile()
f.add_file(binfile)
print(f.as_hexdump())

def _do_as_ihex(args):
for binfile in args.binfile:
f = BinFile()
f.add_file(binfile)
print(f.as_ihex())

def _do_as_srec(args):
for binfile in args.binfile:
f = BinFile()
f.add_file(binfile)
print(f.as_srec())

def _main():
parser = argparse.ArgumentParser(
Expand All @@ -1064,6 +1081,34 @@ def _main():
help='One or more binary format files.')
info_parser.set_defaults(func=_do_info)

# The 'as_hexdump' subparser.
hexdump_parser = subparsers.add_parser(
'as_hexdump',
description='Print given file(s) as hexdumps.')
hexdump_parser.add_argument('binfile',
nargs='+',
help='One or more binary format files.')
hexdump_parser.set_defaults(func=_do_as_hexdump)

# The 'as_srec' subparser.
srec_parser = subparsers.add_parser(
'as_srec',
description='Print given file(s) as Motorola S-records.')
srec_parser.add_argument('binfile',
nargs='+',
help='One or more binary format files.')
srec_parser.set_defaults(func=_do_as_srec)

# The 'as_ihex' subparser.
ihex_parser = subparsers.add_parser(
'as_ihex',
description='Print given file(s) as Intel HEX.')
ihex_parser.add_argument('binfile',
nargs='+',
help='One or more binary format files.')
ihex_parser.set_defaults(func=_do_as_ihex)


args = parser.parse_args()

if args.debug:
Expand Down
71 changes: 50 additions & 21 deletions tests/test_bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,40 +569,69 @@ def test_performance(self):
binfile = bincopy.BinFile()
binfile.add_srec(srec)

def test_command_line_info_help(self):
argv = ['bincopy', 'info', '--help']
output = """usage: bincopy info [-h] binfile [binfile ...]
def test_command_line_help(self):
commands_descriptions = [
('info','Print general information about given file(s).'),
('as_hexdump','Print given file(s) as hexdumps.'),
('as_srec','Print given file(s) as Motorola S-records.'),
('as_ihex','Print given file(s) as Intel HEX.')]

Print general information about given file(s).
for command_name, description in commands_descriptions:
argv = ['bincopy', command_name, '--help']
output = """usage: bincopy {} [-h] binfile [binfile ...]
{}
positional arguments:
binfile One or more binary format files.
optional arguments:
-h, --help show this help message and exit
"""
""".format(command_name,description)

with self.assertRaises(SystemExit) as cm:
self._test_command_line_raises(argv, output)

self.assertEqual(cm.exception.code, 0)


def test_command_line_non_existing_file(self):
commands = ['info', 'as_hexdump', 'as_srec', 'as_ihex']
for command in commands:
argv = ['bincopy', command, 'non-existing-file']
output = ""

with self.assertRaises(SystemExit) as cm:
self._test_command_line_raises(argv, output)

self.assertEqual(cm.exception.code,
"[Errno 2] No such file or directory: 'non-existing-file'")

with self.assertRaises(SystemExit) as cm:
self._test_command_line_raises(argv, output)
def test_command_line_non_existing_file_debug(self):
commands = ['info', 'as_hexdump', 'as_srec', 'as_ihex']
for command in commands:
argv = ['bincopy', '--debug', command, 'non-existing-file']
output = ""

self.assertEqual(cm.exception.code, 0)
with self.assertRaises(IOError):
self._test_command_line_raises(argv, output)

def test_command_line_info_non_existing_file(self):
argv = ['bincopy', 'info', 'non-existing-file']
output = ""

with self.assertRaises(SystemExit) as cm:
self._test_command_line_raises(argv, output)
def test_command_line_dump_commands_one_file(self):
test_file = "tests/files/empty_main.s19"
binfile = bincopy.BinFile(test_file)

self.assertEqual(cm.exception.code,
"[Errno 2] No such file or directory: 'non-existing-file'")
command_func_pairs = [
('as_hexdump',binfile.as_hexdump()),
('as_srec', binfile.as_srec()),
('as_ihex', binfile.as_ihex())
]

def test_command_line_info_non_existing_file_debug(self):
argv = ['bincopy', '--debug', 'info', 'non-existing-file']
output = ""
for command,func in command_func_pairs:

with self.assertRaises(IOError):
self._test_command_line_raises(argv, output)
self._test_command_line_ok(
['bincopy', command, test_file],
func)

def test_command_line_info_one_file(self):
self._test_command_line_ok(
Expand Down Expand Up @@ -665,7 +694,7 @@ def _test_command_line_ok(self, argv, expected_output):
actual_output = sys.stdout.getvalue()
sys.stdout = stdout

self.assertEqual(actual_output, expected_output)
self.assertEqual(actual_output.rstrip(), expected_output.rstrip())


if __name__ == '__main__':
Expand Down

0 comments on commit 2129433

Please sign in to comment.