#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Parsing {
/// Parses and stores an application's command line parameters
///
///
/// At the time of the creation of this component, there are already several command
/// line parsing libraries out there. Most of them, however, do way too much at once
/// or at the very least rely on huge, untested clutters of classes and methods to
/// arrive at their results.
///
///
/// This command line parser does nothing more than represent the command line to
/// the application through a convenient interface. It parses a command line and
/// extracts the arguments, but doesn't interpret them and or check them for validity.
///
///
/// This design promotes simplicity and makes is an ideal building block to create
/// actual command line interpreters that connect the parameters to program
/// instructions and or fill structures in code.
///
///
/// Terminology
///
/// -
/// Command line
///
/// The entire command line either as a string or as
/// an already parsed data structure
///
///
/// -
/// Argument
///
/// Either an option or a loose value (see below) that being specified on
/// the command line
///
///
/// -
/// Option
///
/// Can be specified on the command line and typically alters the behavior
/// of the application or changes a setting. For example, '--normalize' or
/// '/safemode'.
///
///
/// -
/// Value
///
/// Can either sit loosely in the command line (eg. 'update' or 'textfile.txt')
/// or as assignment to an option (eg. '--width=1280' or '/overwrite:always')
///
///
///
///
///
public partial class CommandLine {
/// Initializes a new command line
public CommandLine() {
this.arguments = new List();
}
/// Parses the command line arguments from the provided string
/// String containing the command line arguments
/// The parsed command line
public static CommandLine Parse(string commandLineString) {
bool windowsMode = (Path.DirectorySeparatorChar != '/');
return Parser.Parse(commandLineString, windowsMode);
}
/// Parses the command line arguments from the provided string
/// String containing the command line arguments
/// Whether the / character initiates an argument
/// The parsed command line
public static CommandLine Parse(string commandLineString, bool windowsMode) {
return Parser.Parse(commandLineString, windowsMode);
}
#region To Be Removed
/// Adds a loose value to the command line
/// Value taht will be added
internal void addValue(StringSegment value) {
/*
Console.WriteLine("Discovered loose value: '" + value.ToString() + "'");
*/
this.arguments.Add(
Argument.ValueOnly(value, value.Offset, value.Count)
);
}
/// Adds an argument to the command line
/// Argument that will be added
internal void addArgument(Argument argument) {
/*
Console.WriteLine("Discovered option: '" + argument.Raw.ToString() + "'");
Console.WriteLine(" Name: '" + argument.Name + "'");
if(argument.Value != null) {
Console.WriteLine(" Value: '" + argument.Value + "'");
}
*/
this.arguments.Add(argument);
}
#endregion // To Be Removed
/// Options that were specified on the command line
public IList Arguments {
get { return this.arguments; }
}
/// Options that were specified on the command line
private List arguments;
}
} // namespace Nuclex.Support.Parsing