首页›Adam Johnson›文章

Zsh: parse command-line options with zparseopts

Adam Johnson · 2026年10月28日

AI 摘要 AI 生成,可能有误

本文讲解了如何在 Shell 脚本中使用 Zsh 的内建命令 zparseopts 来解析命令行选项,只需一行声明即可同时处理短选项和长选项。文章通过示例演示了其主要特性:以 name=array 的形式声明选项,使用诸如 -D(从参数中移除已解析的选项)、-E(允许选项出现在任意位置)和 -F(遇到未知选项则报错)等标志,以及带参数的选项(用 : 表示)或累积重复项的选项(用 + 表示)。文章还指出了其局限性,例如不内置支持 --option=value 语法,需要手动去除等号。

Sometimes you want your shell scripts to take options, like --verbose or --help, along with any positional arguments. Handling options is fiddly to do by hand, with the traditional portable approach being a large block of code using a while loop, a case statement, and some shifts. Bash has the getopts builtin, but it only handles single-letter options, like -v. Zsh also has getopts, for compatibility, with the same limitation.

Zsh has a better tool: zparseopts. It handles both short and long options, in one line of declarations.

A simple example

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Adam Johnson

Django-related Deals for Black Friday 2026

AI 摘要This article rounds up Django-related Black Friday and Cyber Monday deals, including 50% discounts on the author's 'Boost Your DX' books, Aidas Bendoraitis' paid Django packages, the book Django in Action from Manning, Cory Zue's SaaS Pegasus, and Talk Python's Everything Bundle. It also mentions 25% off pre-orders of Django 6 By Example and encourages readers to support the Django Software Foundation through donations or corporate sponsorship.

Zsh: measure command durations with the time command, TIMEFMT, and REPORTTIME

AI 摘要This article explains Zsh's built-in `time` command, which reports user/system CPU time, CPU percentage, and elapsed wall-clock time for a command or pipeline, and notes that subshells are needed to time multiple commands or brace groups. It also covers the TIMEFMT parameter for customizing output—including statistics like maximum memory usage and microsecond precision—and the REPORTTIME setting, which automatically prints timing statistics for any command whose CPU time exceeds a specified threshold.

Django: introducing django-launch-editor for jump-to-file debugging

AI 摘要The author introduces django-launch-editor, a new package that turns file locations on Django's debug error pages into clickable links that open your editor directly at the right line and column, saving the hassle of manually copying paths. It builds on launch-editor-py, a Python port (created largely with LLM assistance) of the npm launch-editor package that detects your running editor and knows its command-line syntax, and it was inspired by similar functionality added to the author's profiling-explorer tool. Setup only requires installing the package, setting DEFAULT_EXCEPTION_REPORTER, and adding its URLs, with zero overhead in production; if the editor isn't on the same machine (e.g., Docker or remote servers), it falls back to copying the location to the clipboard.

Zsh: glob case-insensitively with the (#i) glob flag and the no_case_glob option

AI 摘要Zsh globs are case-sensitive by default, but you can make an individual glob case-insensitive by using the (#i) glob flag, which requires enabling the extended_glob option. To make all globs case-insensitive globally, you can enable the no_case_glob option in your ~/.zshrc. Additionally, the case_paths option can be turned on if you want literal directory names to remain case-sensitive while globs match case-insensitively.

Zsh: uncover hidden files in globs with (D)

AI 摘要Zsh globs skip hidden dotfiles by default, but adding the (D) glob qualifier includes them, which is especially useful for recursive globs like **/*.yml(D) to find files inside hidden directories. The glob_dots option in ~/.zshrc can make globs always include hidden files (with (^D) available to exclude them), while patterns starting with a dot list only hidden files. Zsh never matches the special . and .. entries in any of these cases.