我在設置布爾值時遇到了問題。我的代碼是:Python argparse布爾問題
training_pages_list_file = ''
html_page_dir = ''
clf_file_str = ''
user_idf_param = False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Classify some CraigsList Pages')
parser.add_argument('csv_file', action='store')
parser.add_argument('file_dir', action='store')
parser.add_argument('clf_file', action='store')
parser.add_argument('-i', action='store_true', help="include idf", dest=user_idf_param, default=False)
args = parser.parse_args()
然而,這引起了:
hon3.4/argparse.py", line 1721, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1742, in parse_known_args
if not hasattr(namespace, action.dest):
TypeError: hasattr(): attribute name must be string
我怎麼能有這麼如果包括-i
,它將設置user_idf_param
到True
?
'argparse'不設置全局變量的值。它設置'parse_args'返回的'args'對象的屬性。 – hpaulj 2015-03-08 21:19:35
http://argparse4j.sourceforge.net/index.html是一個模擬'argparse'的Java庫。 – hpaulj 2015-03-08 21:34:31