如何编写既接受argparse
命令行参数又接受__init__
参数的程序,以便程序可以在命令行之外使用?我也想知道在导出文件时是否需要显式使用os.getcwd()
。非常感谢你的帮助!
class MyClass(object):
def __init__(self, file='', duplicate_column_name='', file_destination=''):
self.__file1 = file
self.__duplicate = duplicate_column_name
self.__file_dest = file_destination
if __name__ == __main__:
parser = argparse.ArgumentParser(add_help=True, description="Allows users to filter CSV file in a variety of ways.")
parser.add_argument('-f', action='store', dest = 'file', help='Store the name of the csv file you want converted')
parser.add_argument('-c', action='store', dest = 'column', help='Store the name of the column you want filtered.')
parser.add_argument('-d', action='store', dest = 'file_destination', default= str( os.getcwd() ) + "/filteredcsvfile.csv", help= "Store the name of the file you'd like the program to create")
if len(sys.argv) <= 2:
parser.print_help()
sys.exit(1)
cmd_args = parser.parse_args()
file = cmd_args.file
duplicate_column_name = cmd_args.column
file_destination = cmd_args.file_destination