user765443
user765443

Reputation: 1892

Parse command line argument using argparse

I have utility which pass multiple argument alongwith require element Could anyone provide some input How can I handle this scenario using argparse. Please find the sample code

#! /usr/bin/env python
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-cdl", dest = "input_file")
args = parser.parse_args()
print args

Command Line :  python test_6.py -cdl sample (workfine)

utility also pass :  python test_6.py -cdl sample -cdl-sp -cdl-ck

The last two argument for tool.As my program I need to take sample file and ignore rest two argument without any error.In current code, it give me error

Upvotes: -1

Views: 230

Answers (2)

hpaulj
hpaulj

Reputation: 231335

args, rest = parser.parse_known_args()
print args
print rest

rest should equal

['-cdl-sp', '-cdl-ck']

Upvotes: 1

MatthieuW
MatthieuW

Reputation: 2372

You can just add options for the arguments you don't need.

parser.add_argument("-cdl-sp", dest = "sp",  action='store_true')
parser.add_argument("-cdl-sk", dest = "sk",  action='store_true')

Upvotes: 1

Related Questions