Skip to main content

A Python library for splitting command-line arguments according to NT (Windows) and POSIX (Unix) rules. Like `shlex.split` but with NT support and without shell features.

Project description

split-command-line

A Python library for splitting command-line arguments according to NT (Windows) and POSIX (Unix) rules. Like shlex.split but with NT support and without shell features.

Features

  • Two implementations:
    • split_command_line_nt() for NT-style parsing. Mimics MSVCRT's GetCommandLine.
    • split_command_line_posix() for POSIX-style parsing. Behavior closely matches basic POSIX shell parsing but deliberately omits many shell features for safety.
  • No shell features (wildcards, expansion, substitution, etc.) - pure parsing only
  • Readable, state machine-based implementations
  • Python 2 and 3 compatible - works with unicode on both

Installation

pip install split-command-line

Basic Usage

from split_command_line import split_command_line_nt, split_command_line_posix

command_line = u'...'
arguments = list(split_command_line_nt(command_line)) # or list(split_command_line_posix(command_line))

Usage Examples

We can directly execute this module to see how it splits command-line arguments:

python -m split_command_line nt # or python -m split_command_line posix

NT (Windows) Examples

$ python -m split_command_line nt
Enter a command line: %USERNAME%
argv[0]=%USERNAME%

Enter a command line: '%USERNAME%'
argv[0]='%USERNAME%'

Enter a command line: print_argv Hello & print_argv injected
argv[0]=print_argv
argv[1]=Hello
argv[2]=&
argv[3]=print_argv
argv[4]=injected

Enter a command line: print_argv *.txt
argv[0]=print_argv
argv[1]=*.txt

Enter a command line: C:\Users
argv[0]=C:\Users

Enter a command line: C:\Users\
argv[0]=C:\Users\

Enter a command line: C:\Program Files
argv[0]=C:\Program
argv[1]=Files

Enter a command line: "C:\Program Files"
argv[0]=C:\Program Files

Enter a command line: \
argv[0]=\

Enter a command line: \\
argv[0]=\\

Enter a command line: \\\
argv[0]=\\\

Enter a command line: \"
argv[0]="

Enter a command line: \\\"
argv[0]=\"

Enter a command line: \\\\\"
argv[0]=\\"

Enter a command line: "\"\\\""
argv[0]="\"

Enter a command line: "\\\\\"\""
argv[0]=\\""

Enter a command line: "\"abc\" & \"def\""
argv[0]="abc" & "def"

Enter a command line: "\"a&\"b\"c\"d\"\""
argv[0]="a&"b"c"d""

POSIX (Unix) Examples

$ python -m split_command_line posix
Enter a command line: ${variable}
argv[0]=${variable}

Enter a command line: $(command)
argv[0]=$(command)

Enter a command line: abc && rm -rf
argv[0]=abc
argv[1]=&&
argv[2]=rm
argv[3]=-rf

Enter a command line: 'a' "b" c
argv[0]=a
argv[1]=b
argv[2]=c

Enter a command line: 'a'"b"c
argv[0]=abc

Enter a command line: 'a'\b"c"
argv[0]=abc

Enter a command line: foo\ bar
argv[0]=foo bar

Enter a command line: 'a\\b'
argv[0]=a\\b

Enter a command line: 'a\"b'
argv[0]=a\"b

Enter a command line: "a\\b"
argv[0]=a\b

Enter a command line: "a\"b"
argv[0]=a"b

Enter a command line: "a"b"
Unclosed quote/unfinished escape in input

Enter a command line: "a$b"
argv[0]=a$b

Enter a command line: "a\$b"
argv[0]=a$b

Enter a command line: "a`b`c"
argv[0]=a`b`c

Enter a command line: "a\`b\`c"
argv[0]=a`b`c

Enter a command line: 'abc
Unclosed quote/unfinished escape in input

Enter a command line: "abc
Unclosed quote/unfinished escape in input

Enter a command line: abc'
Unclosed quote/unfinished escape in input

Enter a command line: abc"
Unclosed quote/unfinished escape in input

Enter a command line: abc\
Unclosed quote/unfinished escape in input

Differences Between Implementations

Feature NT (Windows) POSIX (Unix)
Quote characters " only ' and "
Backslash escaping Only before quotes All characters
Special characters Treated literally Some special handling
Error handling Silent Raises ValueError

Development

Contributions welcome! Please open issues or pull requests on GitHub.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

split_command_line-0.1.0a0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

split_command_line-0.1.0a0-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file split_command_line-0.1.0a0.tar.gz.

File metadata

  • Download URL: split_command_line-0.1.0a0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for split_command_line-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 fbd0d25afaa2294536d36d1db8292ef1a859abe0911b2507d4c707c7e7759ae2
MD5 e660aff6c88529070c0e9ac23fc7d928
BLAKE2b-256 6d8cdc488759dfb55d407873425b41183b879d05684301a4c231d7d765e7d504

See more details on using hashes here.

File details

Details for the file split_command_line-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for split_command_line-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 81000b0793c5695f3669fd595f844e54764a66afccf5fc8795de48496713e883
MD5 4f89b906eaa2faec24b313d264b96916
BLAKE2b-256 3fa7acf34b1aa1c679dddd95479b16454e3e374130d344751bb208960287dea5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page