<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y" -->

---
title: Zsh: parse command-line options with zparseopts | daily.dev
description: Zsh's zparseopts parses both short and long command-line options in a single declaration, unlike getopts which only handles single-letter flags. A script...
canonical: https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Zsh: parse command-line options with zparseopts | daily.dev
og:description: Zsh's zparseopts parses both short and long command-line options in a single declaration, unlike getopts which only handles single-letter flags. A script...
og:url: https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y
og:image: https://api.daily.dev/og/posts/ZaDmGqD0y.png
og:image:alt: Zsh: parse command-line options with zparseopts
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Zsh: parse command-line options with zparseopts

**[Adam Johnson](https://daily.dev/sources/adamj)** · 5 min read · 0 upvotes · 0 comments

## Summary

Zsh's zparseopts parses both short and long command-line options in a single declaration, unlike getopts which only handles single-letter flags. A script declares specs like {l,-loud}=flag_loud, and parsed options land in named arrays you test with $#flag_loud. Three flags are recommended: -D strips parsed options from $@, -E lets options appear anywhere, and -F fails on unknown options like --lodu. A trailing : marks an option that takes an argument, and a trailing + makes it accumulate across repeats. One gotcha: zparseopts does not support the --number=3 syntax, since the = ends up stuck in the argument value and breaks math expressions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://adamj.eu/tech/2026/10/28/zsh-zparseopts>

## Questions this post answers

### How do I parse both short and long command-line options in a zsh script?

Use the zparseopts builtin with specs like {l,-loud}=flag_loud, which expands to both -l and --loud writing into the flag_loud array. Recommended flags are -D to strip parsed options from $@, -E to allow options anywhere in the argument list, and -F to fail on unknown options with an error message and non-zero exit status.

_daily.dev surfaces shell scripting guides like this for developers hardening their CLI tools._

### How do I make a zsh zparseopts option that takes a value, like --number 3?

Add a colon after the option name in the spec, for example {n,-number}:=arg_number, which appends both the option and its value to the array. Read the value with ${arg_number[-1]:-1}, taking the last element so repeated flags override earlier ones, with a default when the option is absent.

_developers wiring up argument parsing in shell scripts can track patterns like this on daily.dev._

### Why does zsh zparseopts fail on --number=3 syntax?

zparseopts does not treat the equals sign specially for long options, so passing --number=3 leaves '=3' as the literal argument value instead of splitting out 3. This breaks numeric usage like a repeat loop, producing a 'bad math expression' error. Supporting that syntax requires manually stripping a leading '=' with ${name#pattern} parameter expansion.

_anyone debugging CLI argument quirks across shells can find write-ups like this via daily.dev._

## Similar posts on daily.dev

- [Zsh: tab complete glob qualifiers](https://daily.dev/posts/zsh-tab-complete-glob-qualifiers-trzdr8umc) · Adam Johnson · 1 upvotes · 0 comments
- [An old-new take on argument parsing in Rust](https://daily.dev/posts/an-old-new-take-on-argument-parsing-in-rust-ystbyr3b2) · Lobsters · 27 upvotes · 2 comments
- [8 Useful Ways to Configure Your Zsh History — Nick Janetakis](https://daily.dev/posts/8-useful-ways-to-configure-your-zsh-history-nick-janetakis-b94vzinwy) · Nick Janetakis · 10 upvotes · 1 comments
- [Python Command Line Arguments: sys.argv, argparse, getopt](https://daily.dev/posts/python-command-line-arguments-sys-argv-argparse-getopt-ookr1pixv) · DigitalOcean Community · 2 upvotes · 0 comments
- [You probably don't need Oh My Zsh](https://daily.dev/posts/you-probably-don-t-need-oh-my-zsh-h6pnfozld) · Hacker News · 103 upvotes · 15 comments

---

Tags: [#shell](https://daily.dev/tags/shell), [#bash](https://daily.dev/tags/bash)

[View this post on daily.dev](https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Zsh: parse command-line options with zparseopts","url":"https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y"},"datePublished":"2026-10-28T23:57:26.068Z","dateModified":"2026-10-28T23:59:30.667Z","description":"Zsh's zparseopts parses both short and long command-line options in a single declaration, unlike getopts which only handles single-letter flags. A script...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2dfb2402922b372f2efcf4fdc306c33d?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/2dfb2402922b372f2efcf4fdc306c33d?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Adam Johnson","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Adam Johnson","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/bf70f4401c284aa5b628d1966029342c","url":"https://daily.dev/sources/adamj"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"shell,bash","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Adam Johnson","item":"https://daily.dev/sources/adamj"},{"@type":"ListItem","position":3,"name":"Zsh: parse command-line options with zparseopts"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/zsh-parse-command-line-options-with-zparseopts-zadmgqd0y#faq","mainEntity":[{"@type":"Question","name":"How do I parse both short and long command-line options in a zsh script?","acceptedAnswer":{"@type":"Answer","text":"Use the zparseopts builtin with specs like {l,-loud}=flag_loud, which expands to both -l and --loud writing into the flag_loud array. Recommended flags are -D to strip parsed options from $@, -E to allow options anywhere in the argument list, and -F to fail on unknown options with an error message and non-zero exit status. daily.dev surfaces shell scripting guides like this for developers hardening their CLI tools."}},{"@type":"Question","name":"How do I make a zsh zparseopts option that takes a value, like --number 3?","acceptedAnswer":{"@type":"Answer","text":"Add a colon after the option name in the spec, for example {n,-number}:=arg_number, which appends both the option and its value to the array. Read the value with ${arg_number[-1]:-1}, taking the last element so repeated flags override earlier ones, with a default when the option is absent. developers wiring up argument parsing in shell scripts can track patterns like this on daily.dev."}},{"@type":"Question","name":"Why does zsh zparseopts fail on --number=3 syntax?","acceptedAnswer":{"@type":"Answer","text":"zparseopts does not treat the equals sign specially for long options, so passing --number=3 leaves '=3' as the literal argument value instead of splitting out 3. This breaks numeric usage like a repeat loop, producing a 'bad math expression' error. Supporting that syntax requires manually stripping a leading '=' with ${name#pattern} parameter expansion. anyone debugging CLI argument quirks across shells can find write-ups like this via daily.dev."}}]}
```

