This post is about a decade old problem: how to properly encode command line arguments. Properly encoding string arguments into a long command line should not be a complicated issue. However, I have seen multiple situations where people wrongfully assume that wrapping each argument with double quotes is ‘safe enough’ for most situations. That is simply not true.
This article is about the C++ library libArgvCodec which properly encodes and decodes command line parameters.
Skip to the download section for quick download.
The purpose of the library is not to handle Argument Parsing and Argument Validation : there are already multiple libraries that can do that and most of them does it pretty well.
However, few of them are actually developed to help a user passing arguments from one application to another. The libArgvCodec library includes arguments and command line encoder and decoder (codecs) that are designed for this particular purpose.
Transferring local variable values to another application through command line arguments is not something that occurs pretty often but if you ever have to deal with this situation, the libArgvCodec library can help you avoid typical encoding pitfalls.
Here is a list of all library features:
int argc, char* argvargument parsing.
- Supports case sensitive and case insensitive searches within lists of arguments.
- Quickly identify unknown arguments.
- Supports any type of argument prefixes like: slashes, single dash, double dash, etc.
- Encodes multiple single arguments into a full command line.
- Decodes a command line into individual arguments.
- Supports both Windows shell (command prompt) and Windows CreateProcess() encoding/decoding.
- Bulletproof application againts command line injection attacks.
Parsing the list of arguments for validation is easy with the libArgvCodec. Create an instance of the
ArgumentList class which encapsulates a list of argument values. Call the
init() method using the application’s
argv parameters which initialize the instance with a copy of all the parameters.
Browse through the list of arguments using the
getArgument() method or search for a specific parameter using the
Searching for options (argument of the type /p or –help or start) is easily done with the class using the
Search for arguments with values for patterns likes /name=foo or –count=5) using the
Search for name and value pairs (arguments whose value is following as another argument) for patterns like /product foo or –repeat 5 using the
To quickly identify unknown arguments, the class supports extract methods (one for each find method). Extract method removes the argument from the
ArgumentList. Once all know arguments are removed from the list, the remaining ones are considered unknown.
Manipulating an argument list
ArgumentList class is used as a container for arguments. It supports all CRUD operations:
- Create (add) with the
- Read (view) with the
- Update with the
- Delete with the
Encoding arguments and decoding command line
The encoding and decoding of command line arguments is with
IArgumentDecoder interfaces. To interact with the interfaces, the
ArgumentList class is used as input data or output data.
The library provides the following classes which implements both interfaces:
CmdPromptArgumentCodechandles encoding/decoding for the Windows command prompt (shell)
CreateProcessArgumentCodechandles encoding/decoding for the Windows CreateProcess() api.
Using these classes, one can fill up an
ArgumentList instance and call the
encodeCommandLine() method to encode all the arguments in the list into a single command line.
Using the same codec classes, one can ‘split’ a command line into multiple arguments using the
decodeCommandLine() method which returns a valid
ArgumentList with the result.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License (LGPL-3.0) for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
This software is furnished “as is”, without technical support, and with no warranty, express or implied, as to its usefulness for any purpose.
You can download the libArgvCodec library by clicking on the following link: