Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7162947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:49:07+00:00 2026-05-28T13:49:07+00:00

I have written a command-line tool that uses sub-commands much like Mercurial, Git, Subversion

  • 0

I have written a command-line tool that uses sub-commands much like Mercurial, Git, Subversion &c., in that its general usage is:

>myapp [OPTS] SUBCOMMAND [SUBCOMMAND-OPTS] [ARGS]

E.g.

>myapp --verbose speak --voice=samantha --quickly "hello there"

I’m now in the process of building Zsh completion for it but have quickly found out that it is a very complex beast. I have had a look at the _hg and _git completions but they are very complex and different in approach (I struggle to understand them), but both seem to handle each sub-command separately.

Does anyone know if there a way using the built in functions (_arguments, _values, pick_variant &c.) to handle the concept of sub-commands correctly, including handling general options and sub-command specific options appropriately? Or would the best approach be to manually handle the general options and sub-command?

A noddy example would be very much appreciated.

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:49:08+00:00Added an answer on May 28, 2026 at 1:49 pm

    Writing completion scripts for zsh can be quite difficult. Your best bet is to use an existing one as a guide. The one for Git is way too much for a beginner. You can use this repo:

    https://github.com/zsh-users/zsh-completions

    As for your question, you have use the concept of state. You define your subcommands in a list and then identify via $state which command you are in. Then you define the options for each command. You can see this in the completion script for play. A simplified version is below:

    _play() {
      local ret=1
    
      _arguments -C \
        '1: :_play_cmds' \
        '*::arg:->args' \
      && ret=0
    
      case $state in
        (args)
           case $line[1] in
             (build-module|list-modules|lm|check|id)
               _message 'no more arguments' && ret=0
             ;;
             (dependencies|deps)
               _arguments \
                 '1:: :_play_apps' \
                 '(--debug)--debug[Debug mode (even more informations logged than in verbose mode)]' \
                 '(--jpda)--jpda[Listen for JPDA connection. The process will  suspended until a client is plugged to the JPDA port.]' \
                 '(--sync)--sync[Keep lib/ and modules/ directory synced. Delete unknow dependencies.]' \
                 '(--verbose)--verbose[Verbose Mode]' \
                 && ret=0
             ;;
           esac
       esac
    

    (If you are going to paste this, use the original source, as this won’t work).

    It looks daunting, but the general idea is not that complicated:

    • The subcommand comes first (_play_cmds is a list of subcommands with a description for each one).
    • Then come the arguments. The arguments are built based on which subcommand you are choosing. Note that you can group multiple subcommands if they share arguments.

    With man zshcompsys, you can find more info about the whole system, although it is somewhat dense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C command line tool written in Xcode that uses the Accelerate
I have written a tool that is run on the command line. I can
I have a small command-line application written in C that acts as a wrapper/launcher
I have a project that has a GUI (written in QT) and a command-line
Summary I have written a process monitor command-line application that takes as parameters: The
I have written a Java application that runs from the command line. I want
I've written a command line utility that uses getopt for parsing arguments given on
I've written a command line tool that preprocesses a number of files then compiles
I have written a C++ program (which executes from the command line), that works
I have written a Windows application, that can take in some command line arguments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.