Git Product home page Git Product logo

winbox-args's Introduction

Winbox-Args

Build Status Build status

A PHP function to escape command-line arguments, which replaces escapeshellarg with more robust methods for both Windows and non-Windows platforms. Install from Packagist and use it like this:

$escaped = Winbox\Args::escape($argument);

Alternatively, you can just copy the code into your own project (but please keep the license attribution and documentation link).

What it does on the Windows platform

The following transformations are made:

  • Double-quotes are escaped with a backslash, with any preceeding backslashes doubled up.
  • The argument is only enclosed in double-quotes if it contains whitespace or is empty.
  • Trailing backslashes are doubled up if the argument is enclosed in double-quotes.

See How Windows parses the command-line if you would like to know why.

By default, cmd.exe meta characters are also escaped:

  • by caret-escaping the transformed argument (if it contains internal double-quotes or %...% syntax).
  • or by enclosing the argument in double-quotes.

There are some limitations:

  1. If cmd is started with DelayedExpansion enabled, !...! syntax could expand environment variables.
  2. If the program name requires caret-escaping and contains whitespace, cmd will not recognize it.
  3. If an argument contain a newline \n character, this will not be escaped.

See How cmd.exe parses a command and Implementing a solution for more information.

What it does on non-Windows platforms

The argument is enclosed is single-quotes, with internal single-quotes escaped.

Is that it?

Yup. An entire repo for a tiny function. However, it needs quite a lot of explanation because:

  • the command-line parsing rules in Windows are not immediately obvious.
  • PHP generally uses cmd.exe to execute programs and this applies a different set of rules.
  • there is no simple solution.

Full details explaining the different parsing rules, potential pitfalls and limitations can be found in the Wiki.

License

Winbox-Args is licensed under the MIT License - see the LICENSE file for details.

winbox-args's People

Contributors

johnstevenson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.