Thanks to visit codestin.com
Credit goes to github.com

Skip to content
forked from petamas/oslex

OS-independent wrapper for shlex and mslex

License

Notifications You must be signed in to change notification settings

jessielw/oslex2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

oslex2

CI PyPI version

oslex2 is an OS-independent wrapper for Python's shlex (POSIX) and mslex (Windows) modules.

Like os.path abstracts over posixpath and ntpath, oslex2 abstracts over shlex and mslex for shell-quoting and splitting.

Features

  • Unified API for shell quoting, splitting, and joining on all platforms
  • Automatically uses shlex on POSIX and mslex on Windows
  • Drop-in replacement for shlex in cross-platform code

Installation

pip install oslex2

Usage

import oslex2

# Safely quote a string for the shell
cmd = oslex2.quote('foo; rm -rf /')

# Split a shell command into arguments
args = oslex2.split('python -m pip install "oslex2>=1.0.0"')

# Join arguments into a shell command
command = oslex2.join(["python", "-m", "pip", "install", "oslex2>=1.0.0"])

API

  • oslex2.quote(s: str, **kwargs) -> str: Shell-escape a string for safe use as a single token.
  • oslex2.split(s: str, **kwargs) -> List[str]: Split a shell command string into arguments.
  • oslex2.join(args: List[str]) -> str: Join arguments into a shell-escaped command string.

Note: All functions accept **kwargs and pass them through to the underlying shlex or mslex implementation. This allows you to use platform-specific options (such as posix, punctuation_chars, etc.) as needed.

Platform Detection

  • On Windows, uses mslex for Windows shell syntax.
  • On all other platforms, uses the standard library shlex.

License

  • MIT License (this library)
  • mslex is Apache 2.0

Links