Skip to content

powershell: no positional completion after a flag that takes no value #268

Done

Description

@ekalinin

With shtab 1.13.0, the PowerShell script completes nothing for a
positional argument that comes after a flag such as --force
(store_true).

import argparse

import shtab

parser = argparse.ArgumentParser(prog='demo')
parser.add_argument('--force', action='store_true')
parser.add_argument('dest').complete = shtab.DIRECTORY
print(shtab.complete(parser, 'powershell'))

Going by the generated code, demo <Tab> offers directories and
demo --force <Tab> offers nothing; nodeenv's CI showed the latter (see
below). The script gives --force nargs '0':

    '_shtab_demo___force' = '0'

When the completer meets an option string, it switches to that option and
goes on to the next token:

      if ($currentOptions -and $currentOptions -contains $token) {
        $currentActionKey = $prefix + '_' + (_shtab_powershell_replace_nonword $token)
        $currentActionNargs = Get-ActionNargs $currentActionKey
        $currentActionArgsConsumed = 0
        $currentActionIsPositional = $false
        continue
      }

It only moves on to the next positional once the option has consumed
its arguments, and an option with nargs 0 never consumes one. Since an
empty result returns $null, PowerShell does not fall back to file names
either. The bash script handles this: after an option string matches, the
same >= nargs check runs at once and moves to the next positional.

This is from nodeenv (ekalinin/nodeenv#460), where
nodeenv --force <Tab> came back empty in pwsh on the GitHub ubuntu,
macOS and Windows runners, while nodeenv -p <Tab> offered directories.
The duplicate keys of #267 had been worked around in that run.

Activity

  1. added a commit that references this issue on Nov 27, 2026
    76a7649
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions