#region Copyright 2009-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
namespace CSharpTest.Net.Commands
{
///
/// Used for obtaining input directly from user rather than from the std:in stream
///
public delegate Char ReadNextCharacter();
///
/// Defines the interface for the command interpreter. If you use this as a parameter
/// it will be provided auto-magically to your command. To avoid conflicts with ordinal
/// argument matching, make this your last argument.
///
public interface ICommandInterpreter
{
///
/// Gets/sets the exit code of the operation/process
///
int ErrorLevel { get; set; }
///
/// Gets/sets the prompt, use "$(OptionName)" to reference options
///
string Prompt { get; set; }
///
/// Lists all the commands that have been added to the interpreter
///
ICommand[] Commands { get; }
///
/// Returns true if the command was found and cmd output parameter is set.
///
bool TryGetCommand(string name, out ICommand cmd);
///
/// Lists all the options that have been added to the interpreter, use the set/get commands
/// to modify their values.
///
IOption[] Options { get; }
///
/// Returns true if the command was found and cmd output parameter is set.
///
bool TryGetOption(string name, out IOption cmd);
///
/// Command to get an option value by name
///
object Get(string property);
///
/// Command to set the value of an option value by name
///
void Set(string property, object value);
///
/// Run the command whos name is the first argument with the remaining arguments provided to the command
/// as needed.
///
void Run(params string[] arguments);
///
/// Run the command whos name is the first argument with the remaining arguments provided to the command
/// as needed.
///
void Run(string[] arguments, TextWriter mapstdout, TextWriter mapstderr, TextReader mapstdin);
///
/// Runs each line from the reader until EOF, can be used with Console.In
///
void Run(System.IO.TextReader input);
///
/// Expands '$(OptionName)' within the input string to the named option's value.
///
string ExpandOptions(string input);
///
/// Reads a keystroke, not from the std:in stream, rather from the console or ui.
///
ReadNextCharacter ReadNextCharacter { get; }
///
/// Returns an HTML document for help on all items (when item == null) or a specific item.
///
string GetHtmlHelp(string item);
}
///
/// Defines an interface that allows a command filter to call to next filter in the chain
///
public interface ICommandChain
{
///
/// Calls the next command filter in the chain, eventually processing the command
///
void Next(string[] arguments);
}
/// A base interface that provides name and display information
public interface IDisplayInfo
{
/// Returns the type this was reflected from, or null if created without reflection
Type ReflectedType { get; }
/// Returns the display name of the item
string DisplayName { get; }
/// Returns the name of the item
string[] AllNames { get; }
/// Returns the category if defined, or the type name if not
string Category { get; }
/// Returns the description of the item
string Description { get; }
/// Returns true if the items should be displayed.
bool Visible { get; set; }
/// Dynamically adds an attribute to the item.
void AddAttribute(T attribute) where T : Attribute;
/// Returns true if the attribute was found
bool TryGetAttribute(out T found) where T : Attribute;
/// Renders the help information to Console.Out
void Help();
}
///
/// Represents a static or instance method that will be invoked as a command
///
public interface IArgument : IDisplayInfo
{
/// Returns true if the argument is required
bool Required { get; }
/// Returns the default value if Required == false
object DefaultValue { get; }
/// Returns the type of the argument
Type Type { get; }
}
///
/// Represents a static or instance method that will be invoked as a command
///
public interface ICommand : IDisplayInfo
{
/// Returns the arguments defined on this command.
IArgument[] Arguments { get; }
/// Runs this command with the supplied arguments
void Run(ICommandInterpreter interpreter, string[] arguments);
}
///
/// Represents a static or instance method that is used to filter or pre/post process commands
///
public interface ICommandFilter : ICommand
{
/// Returns the possible character keys for this filter when setting the precedence
Char[] Keys { get; }
///
/// Used to run a command through a set of filters, call chain.Next() to continue processing
///
/// The command interpreter running the command
/// The next link in the chain of filters
/// The input arguments to the command-line
void Run(ICommandInterpreter interpreter, ICommandChain chain, string[] arguments);
}
///
/// Defines an Option that can be configued/set independantly of the commands. Used with the set/get
/// commands defined by the interpreter.
///
public interface IOption : IDisplayInfo
{
///
/// Gets/sets the value of the option
///
object Value { get; set; }
/// Returns the type of the option value
Type Type { get; }
}
// Internal sorter for display name
class OrderByName : IComparer
where T : IDisplayInfo
{
int IComparer.Compare(T a, T b)
{ return StringComparer.OrdinalIgnoreCase.Compare(a.DisplayName, b.DisplayName); }
}
}