python 커맨드라인 프로그램 만들기 : argparse

python에서 커맨드라인 프로그램은 argparse를 import해서 쉽게 만들 수 있다.

import argparse

import os
import sys

my_parser = argparse.ArgumentParser(description="List the content of a folder",
                                    epilog="Enjoy the program! :)",
                                    prefix_chars='/')

my_parser.add_argument("Path",
                       metavar="path",
                       type=str,
                       help="the path to list")

args = my_parser.parse_args()

input_path = args.Path

if not os.path.isdir(input_path):
    print("The path specified does not exist")
    sys.exit()

print('\n'.join(os.listdir(input_path)))

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다