From 382bd27dc9effa9adca92efc5c28fcad3db07f08 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Tue, 13 Feb 2018 07:55:52 +1100 Subject: [PATCH] Tidy up command line argument parsing --- scripts/dxf_to_csv.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/scripts/dxf_to_csv.py b/scripts/dxf_to_csv.py index ebf66bb..5eee7cc 100644 --- a/scripts/dxf_to_csv.py +++ b/scripts/dxf_to_csv.py @@ -31,8 +31,7 @@ def point_to_row(point, layer, dxf_type, dxf_id): return row -def convert(dxf_name, layer_name): - # Read dxf file +def print_layers(dxf_name): dxf = dxfgrabber.readfile(dxf_name) # Get data from DTM @@ -41,6 +40,11 @@ def convert(dxf_name, layer_name): for layer in layers: print(layer) + +def convert(dxf_name, layer_name): + # Read dxf file + dxf = dxfgrabber.readfile(dxf_name) + if layer_name: print("\nExtracting data from layer '{}'".format(layer_name)) entities = [ @@ -86,13 +90,26 @@ def convert(dxf_name, layer_name): def main(): - dxf_name = sys.argv[1] - try: - layer_name = sys.argv[2] - except IndexError: - layer_name = None - - convert(dxf_name, layer_name) + # Set up command line arguments + parser = argparse.ArgumentParser() + parser.add_argument('dxf_name', help='path to dxf file', default=None) + parser.add_argument( + '-n', metavar='LAYER_NAME', help='name of layer', default=None) + parser.add_argument( + '-l', action='store_true', help='show list of layers', default=False) + args = parser.parse_args() + + # Parse arguments + dxf_name = args.dxf_name + layer_name = args.n + show_layers_only = args.l + + if show_layers_only: + # Print list of layers only + print_layers(dxf_name) + else: + # Convert to csv + convert(dxf_name, layer_name) if __name__ == '__main__':