Chapter 08
Command-Line Interface
命令行接口
Scripts become tools. sys.argv for the raw approach, argparse for proper ones — with --help, type validation, defaults, and exit codes for free. Build a Unix wc clone.
Strict source: ATBS Appendix B + Python stdlib sys and argparse
Note: browser playgrounds can't run with real sys.argv. The function-takes-argv pattern (parse_args([...])) works everywhere.
Lessons in this chapter
- 08.00 Worked ExampleWalkthrough: sys.argv Greeter例题: 命令行问候python-cmdline8 min
- 08.01 ExerciseSum from argv命令行求和python-cmdline15 min
- 08.02 Exerciseargparse Basicsargparse 入门python-cmdline20 min
- 08.03 Exerciseargparse Defaultsargparse 默认值python-cmdline20 min
- 08.04 HomeworkHomework: wc Clone作业: wc 克隆python-cmdline python-file-io python-functions45 min