ProgramOptions.cs :  » Development » Jayrock » TidyJson » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » Jayrock 
Jayrock » TidyJson » ProgramOptions.cs
namespace TidyJson{
    #region Imports

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;

    #endregion

    internal sealed class ProgramOptions
    {
        public EventHandler Help;
        public JsonPalette Palette;

        public string[] Parse(string[] args)
        {
            Debug.Assert(args != null);

            var inputs = new Queue<string>(args);
            var anonymous = new Queue<string>(args.Length);

            char? altNamedToken = null;

            if (Path.DirectorySeparatorChar != '/')
                altNamedToken = '/';

            while (inputs.Count > 0)
            {
                var arg = DequeueSafely(inputs);

                if (arg.Length > 1 && (arg[0] == '-' || (altNamedToken.HasValue && arg[0] == altNamedToken.Value)))
                {
                    var parts = arg.Split(new[] { '=', ':' }, 2);
                    var name = parts[0].TrimStart(arg[0]);

                    if (name.Length == 0)
                        break;

                    var value = parts.Length > 1 ? parts[1] : string.Empty;

                    switch (name)
                    {
                        case "p":
                        case "palette":
                        {
                            if (value.Length == 0)
                                continue;

                            if (value[0] != '{')
                                value = "{" + value + "}";

                            Palette.ImportJson(value);
                            break;
                        }

                        case "m":
                        case "mono":
                        {
                            if (Convert.ToBoolean(Mask.EmptyString(value, Boolean.TrueString)))
                                Palette = new JsonPalette(ConsoleBrush.Current);
                            break;
                        }

                        case "?":
                        case "help":
                        {
                            if (Help != null)
                                Help(this, EventArgs.Empty);
                            break;
                        }

                        default:
                        {
                            throw new ApplicationException(string.Format("Unknown option '{0}'.", arg));
                        }
                    }
                }
                else
                {
                    anonymous.Enqueue(arg);
                }
            }

            args = anonymous.ToArray();
            return args;
        }

        public static void ShowUsage()
        {
            ShowUsage(null);
        }

        public static void ShowUsage(TextWriter output)
        {
            output = output ?? Console.Out;

            output.WriteLine(
                @"Usage: [OPTION]... FILE

where OPTION is one or more of:

--help              print this help
--mono=BOOL         no syntax coloring
--palette=SCHEME    set the color palette

To set the color palette, use the following syntax for the scheme:

    [ BRUSH = COLOR [ , BRUSH = COLOR ] ]

where BRUSH may be:

    Null
    Boolean
    Number 
    String 
    Object
    Array

and COLOR may be:

    {0}
",
                string.Join(Environment.NewLine + new string(' ', 4),
                    Enum.GetNames(typeof(ConsoleColor))));
        }

        private static T DequeueSafely<T>(Queue<T> queue)
        {
            return queue.Count > 0 ? queue.Dequeue() : default(T);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.