Python

optparse 코드를 argparse 로 마이그레이션

argparse 모듈은 optparse 모듈에서 기본적으로 제공하지 않는 여러 고수준 기능을 포함하며, 다음과 같은 것들이 있습니다:

  • 위치 인자 처리.

  • 서브커맨드 지원.

  • +/ 와 같은 대체 옵션 접두사 허용.

  • 0개 이상 또는 1개 이상의 스타일의 인자 처리.

  • 더 유용한 정보가 포함된 사용법 메시지 생성.

  • 사용자 정의 typeaction 을 위한 훨씬 더 간단한 인터페이스 제공.

Originally, the argparse module attempted to maintain compatibility with optparse. However, the fundamental design differences between supporting declarative command line option processing (while leaving positional argument processing to application code), and supporting both named options and positional arguments in the declarative interface mean that the API has diverged from that of optparse over time.

As described in 인자 파싱 라이브러리 선택하기, applications that are currently using optparse and are happy with the way it works can just continue to use optparse.

마이그레이션을 고려 중인 애플리케이션 개발자는 마이그레이션의 필요성을 결정하기 전에 해당 섹션에 기술된 본질적인 동작 차이 목록을 검토해야 합니다.

optparse 에서 argparse 로 마이그레이션을 선택한 애플리케이션을 위해 다음 제안들이 도움이 될 것입니다.