// Copyright (c) 2007 Omer Rauchwerger (a.k.a rauchy) (omer@rauchy.net)
// All rights reserved.
//
// This file is part of Regionerate.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
namespace Rauchy.Regionerate.Presentation.Console
{
///
/// Provides access to arguments provided by the user which are
/// neccesary for the opeartion of the console application.
///
internal class Arguments
{
#region Fields (7)
private readonly string[] _ignoredFiles;
private readonly string[] _ignoredProjects;
private readonly string _customCodeLayoutPath;
private readonly bool _noInfo;
private readonly bool _noWarn;
private readonly bool _showHelp;
private readonly string _targetFile;
private readonly TargetType? _targetType;
private readonly bool _useDefaultCodeLayout;
#endregion
#region Constructors (1)
///
/// Initializes a new instance of the class.
///
/// if set to true [show help].
/// The target file.
/// Type of the target.
/// if set to true [use default code layout].
/// The custom code layout path.
/// if set to true [show information].
/// if set to true [show warnings].
/// The ignored projects.
/// The ignored files.
private Arguments( bool showHelp,
string targetFile,
TargetType? targetType,
bool useDefaultCodeLayout,
string customCodeLayoutPath,
bool showInformation,
bool showWarnings,
string[] ignoredProjects,
string[] ignoredFiles )
{
_showHelp = showHelp;
_targetFile = targetFile;
_targetType = targetType;
_useDefaultCodeLayout = useDefaultCodeLayout;
_customCodeLayoutPath = customCodeLayoutPath;
_noInfo = showInformation;
_noWarn = showWarnings;
_ignoredProjects = ignoredProjects;
_ignoredFiles = ignoredFiles;
}
#endregion
#region Properties (7)
///
/// Gets the custom code layout path.
///
public string CustomCodeLayoutPath
{
get
{
return _customCodeLayoutPath;
}
}
///
/// Gets a value indicating whether information prompts should be muted.
///
public bool NoInfo
{
get
{
return _noInfo;
}
}
///
/// Gets a value indicating whether warning prompts should be muted.
///
public bool NoWarn
{
get
{
return _noWarn;
}
}
///
/// Gets a value indicating whether to show help or not.
///
public bool ShowHelp
{
get
{
return _showHelp;
}
}
///
/// Gets the target file.
///
/// The target file.
public string TargetFile
{
get
{
return _targetFile;
}
}
///
/// Gets the type of the target.
///
public TargetType? TargetType
{
get
{
return _targetType;
}
}
///
/// Gets a value indicating whether to use default code layout or not.
///
public bool UseDefaultCodeLayout
{
get
{
return _useDefaultCodeLayout;
}
}
///
/// Gets the ignored files.
///
public string[] IgnoredFiles
{
get
{
return _ignoredFiles;
}
}
///
/// Gets the ignored projects.
///
public string[] IgnoredProjects
{
get
{
return _ignoredProjects;
}
}
#endregion
#region Methods (2)
///
/// Parses the specified arguments.
///
/// The arguments.
/// A new instance of the class with the provided arguments.
public static Arguments Parse( string[] args )
{
bool showHelp = false;
string targetFile = string.Empty;
TargetType? targetType = null;
string customCodeLayoutPath = string.Empty;
bool useDefaultCodeLayout = true;
bool noInfo = false;
bool noWarn = false;
string[] ignoredFiles = new string[] {};
string[] ignoredProjects = new string[] {};
foreach ( string arg in args )
{
if ( ( arg == "/?" ) ||
( arg == "-?" ) )
{
showHelp = true;
break;
}
else if ( arg.ToLower().StartsWith( "-target:" ) )
{
targetFile = arg.Remove( 0, "-target:".Length );
if ( targetFile.ToLower().EndsWith( ".cs" ) )
{
targetType = Console.TargetType.File;
}
else if ( targetFile.ToLower().EndsWith( ".csproj" ) )
{
targetType = Console.TargetType.Project;
}
else if ( targetFile.ToLower().EndsWith( ".sln" ) )
{
targetType = Console.TargetType.Solution;
}
else
{
string message = string.Format( "{0}'s file type is not supported", targetFile );
throw new ArgumentException( message );
}
}
else if ( arg.ToLower().StartsWith( "-layout:" ) )
{
useDefaultCodeLayout = false;
customCodeLayoutPath = arg.Remove( 0, "-layout:".Length );
}
else if ( arg.ToLower().StartsWith( "-noinfo" ) )
{
noInfo = true;
}
else if ( arg.ToLower().StartsWith( "-nowarn" ) )
{
noWarn = true;
}
else if ( arg.ToLower().StartsWith( "-ignorefiles:" ) )
{
string files = arg.Remove( 0, "-ignoreFiles:".Length ).Replace( "\"", string.Empty );
ignoredFiles = files.Split( '|' );
}
else if ( arg.ToLower().StartsWith( "-ignoreprojects:" ) )
{
string files = arg.Remove( 0, "-ignoreProjects:".Length ).Replace( "\"", string.Empty );
ignoredProjects = files.Split( '|' );
}
}
return new Arguments( showHelp, targetFile, targetType, useDefaultCodeLayout, customCodeLayoutPath, noInfo,
noWarn, ignoredProjects, ignoredFiles );
}
///
/// Tries to parse the argument.
///
/// The arguments.
/// True if the arguments are valid; False otherwise.
public static bool TryParse( string[] args )
{
// Arguments are valid only when there is at least one
// parameter which is "/?" or "-?" or starts with "-target:".
if ( args.Length == 0 )
{
return false;
}
else
{
foreach ( string arg in args )
{
if ( ( arg == "/?" ) || ( arg == "-?" ) ||
arg.ToLower().StartsWith( "-target:" ) )
{
return true;
}
}
return false;
}
}
#endregion
}
}