Skip to content

Commit

Permalink
Lots of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Dec 2, 2014
1 parent c06378a commit 02acf5a
Show file tree
Hide file tree
Showing 14 changed files with 814 additions and 45 deletions.
7 changes: 7 additions & 0 deletions HappyFunTimes.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HappyFunTimes", "HappyFunTimes\HappyFunTimes.csproj", "{554A0027-C03D-4D04-946D-2A34E43CC586}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HappyFunTimesEditor", "HappyFunTimesEditor\HappyFunTimesEditor.csproj", "{97F1A61A-1B41-4878-BA21-9A029AA49791}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -13,8 +15,13 @@ Global
{554A0027-C03D-4D04-946D-2A34E43CC586}.Debug|Any CPU.Build.0 = Debug|Any CPU
{554A0027-C03D-4D04-946D-2A34E43CC586}.Release|Any CPU.ActiveCfg = Release|Any CPU
{554A0027-C03D-4D04-946D-2A34E43CC586}.Release|Any CPU.Build.0 = Release|Any CPU
{97F1A61A-1B41-4878-BA21-9A029AA49791}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97F1A61A-1B41-4878-BA21-9A029AA49791}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97F1A61A-1B41-4878-BA21-9A029AA49791}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97F1A61A-1B41-4878-BA21-9A029AA49791}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = HappyFunTimes\HappyFunTimes.csproj
version = 0.2
EndGlobalSection
EndGlobal
133 changes: 133 additions & 0 deletions HappyFunTimes/ArgParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2014, Gregg Tavares.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Gregg Tavares. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Collections.Generic;

namespace HappyFunTimes {

/// <summary>
/// A very simple commandline argument parser.
///
/// It just looks arguments that start with "--". For any
/// argument that starts with "--" it will then look for the
/// first "=" sign. If found it will set remember the value
/// for that option. If no equals is found it uses the value
/// "true"
/// </summary>
/// <example>
/// Example: Assume your command line is
/// <code>
/// myprogram --name=gregg --count=123 --debug
/// </code>
/// You can then read those options with
/// <code>
/// using HappyFunTimes;
///
/// ...
/// ArgParser p = new ArgParser();
///
/// // Set Defaults
/// string name = "Someone";
/// int count = 1;
/// bool debug = false;
///
/// p.TryGet<string>("name", name);
/// p.TryGet<int>("count", count);
/// if (p.Contains("debug")) {
/// debug = true;
/// }
/// </code>
/// </example>
/// <returns></returns>
public class ArgParser {

/// <summary>
/// Constructor for ArgParser that parses command line arguments
/// </summary>
public ArgParser()
{
Init(System.Environment.GetCommandLineArgs());
}

/// <summary>
/// Constructor for ArgParser which you can pass your own array of strings.
/// </summary>
/// <param name="args">Array of command line like argument strings</param>
public ArgParser(string[] args)
{
Init(args);
}

/// <summary>
/// Check if a switch exists.
/// </summary>
/// <param name="id">name of switch</param>
/// <returns>true if switch was contained in arguments</returns>
public bool Contains(string id)
{
return m_switches.ContainsKey(id);
}

/// <summary>
/// Gets the value of a switch if it exists
/// </summary>
/// <param name="id">id of switch</param>
/// <param name="value">variable to receive the value</param>
/// <returns>true if the value exists. False if it does not. If false value has not been affected.</returns>
public bool TryGet<T>(string id, ref T value) {
string v;
bool found = m_switches.TryGetValue(id, out v);
if (found) {
value = (T)System.Convert.ChangeType(v, typeof(T));
}
return found;
}

private void Init(string[] arguments)
{
m_switches = new Dictionary<string, string>();
foreach (string arg in arguments) {
if (arg.StartsWith("--")) {
int equalsNdx = arg.IndexOf('=');
if (equalsNdx >= 0) {
m_switches[arg.Substring(2, equalsNdx - 2)] = arg.Substring(equalsNdx + 1);
} else {
m_switches[arg.Substring(2)] = "true";
}
}
}
}

private Dictionary<string, string> m_switches;
};


} // namespace HappyFunTimes

6 changes: 3 additions & 3 deletions HappyFunTimes/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
// Change them to the values specific to your project.

[assembly: AssemblyTitle("HappyFunTimes")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription(".NET Support for HappyFunTimes")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCompany("Greggman")]
[assembly: AssemblyProduct("HappyFunTimes")]
[assembly: AssemblyCopyright("Gregg Tavares")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
Loading

0 comments on commit 02acf5a

Please sign in to comment.