X Tutup
The Wayback Machine - https://web.archive.org/web/20260321200919/https://github.com/python/cpython/issues/94417
Skip to content

argparse handles empty string differently on Linux and Windows 馃檧#94417

@AlexPykavy

Description

@AlexPykavy

Bug report

Consider the code

import argparse
import sys

print(f"argv = {sys.argv}")

parser = argparse.ArgumentParser(description='Print value.')
parser.add_argument('--value', type=str, dest="value")

args = parser.parse_args()
print(f"Passed --value = '{args.value}'")

Running as:

python temp.py --value ''

Result on Windows:

argv = ['temp.py', '--value']
usage: temp.py [-h] [--value VALUE]
temp.py: error: argument --value: expected one argument

Result on Linux (I used Ubuntu from WSL):

argv = ['temp.py', '--value', '']
Passed --value = ''

As a simple solution, we can just remove empty values from the argv to have the same initial list of arguments on both OSes. If we need to preserve the existing logic of taking empty string on Linux, we may implement a smarter algorithm. Feel free to hint me which way we will go if any. I'll be honored to contribute to Python 馃槉.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup