Run a python script with command line arguments

Hi,

I am trying to run a python script that accepts command line arguments as follows.

./Slicer --python-script /home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop

and the python script looks as follows

def extract_centerlines(args):
...

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Extract centerlines from a dataset")
    parser.add_argument('--data_dir', type=str, help='Path to the dataset directory')
    parser.add_argument('--end_points', action='store_true', default=False, help='Save the endpoints')
    parser.add_argument('--network', action='store_true', default=False, help='Save the network model')
    parser.add_argument('--network_curve', action='store_true', default=False, help='Save the network curve')
    parser.add_argument('--network_prop', action='store_true', default=False, help='Save the network properties')
    parser.add_argument('--centerline', action='store_true', default=False, help='Save the centerline model')
    parser.add_argument('--centerline_curve', action='store_true', default=False, help='Save the centerline curve')
    parser.add_argument('--centerline_prop', action='store_true', default=False, help='Save the centerline properties')
    args = parser.parse_args()
    
    extract_centerlines(args)

when I run this script using the command above, I get

./Slicer --python-script /home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop
>

guessing that it’s expecting something more. I also tried

./Slicer --python-script "/home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop"

but I get the error

./Slicer --no-splash --no-main-window --python-script "/home/user/extract_centerlines_vmtk.py --data_dir /PATH/TO/DATA --end_points --network --network_curve --network_prop --centerline --centerline_curve --centerline_prop"

How to run this script with the arguments?

@RomanStriker, check this post and see if it helps:

1 Like

Thanks. I can use the sys.argv as follows

./Slicer --python-script /home/user/extract_centerlines_vmtk.py "/home/user/data" end_points network network_curve network_prop centerline centerline_curve centerline_prop

and in the script

def extract_centerlines(args):
...

if __name__ == '__main__':
    # create args object
    args = argparse.Namespace(
        dataset_dir=sys.argv[1],
        end_points=True if 'end_points' in sys.argv else False,
        network=True if 'network' in sys.argv else False,
        network_curve=True if 'network_curve' in sys.argv else False,
        network_prop=True if 'network_prop' in sys.argv else False,
        centerline=True if 'centerline' in sys.argv else False,
        centerline_curve=True if 'centerline_curve' in sys.argv else False,
        centerline_prop=True if 'centerline_prop' in sys.argv else False,
    )
    
    extract_centerlines(args)