Click here to Skip to main content
15,882,114 members
Articles / Programming Languages / C#

Enumeration-based Command Line Utility

Rate me:
Please Sign up or sign in to vote.
4.99/5 (49 votes)
14 May 2017CPOL19 min read 97.2K   924   101  
An example the application of Enumeration class, third article of the series
namespace CommandLineTest {
    using System.Windows.Forms;
    using SA.Universal.Enumerations;
    using SA.Universal.Utilities;
    using StringBuilder = System.Text.StringBuilder;

    internal class CommandLineReport {

        internal CommandLineReport(ListBox options, ListBox errors) {
            this.Options = options; this.Errors = errors;
        } //CommandLineReport

        internal void Show<SWITCHES, VALUES>(SA.Universal.Utilities.CommandLine<SWITCHES, VALUES> commandLine) {
            Options.Items.Clear();
            Errors.Items.Clear();
            Options.Items.Add(string.Empty);
            Options.Items.Add(DefinitionSet.SectionSwitches);
            foreach (EnumerationItem<SWITCHES> item in commandLine.SwitchEnumeration) {
                CommandLineSwitchStatus status = commandLine[item.EnumValue];
                Options.Items.Add(string.Format(DefinitionSet.FmtSwitch, GenerateDoubleName<SWITCHES>(item), status));
            } //loop
            Options.Items.Add(string.Empty);
            Options.Items.Add(DefinitionSet.SectionValues);
            foreach (EnumerationItem<VALUES> item in commandLine.ValueEnumeration) {
                string value = commandLine[item.EnumValue];
                string valueDisambiguation = string.Empty;
                if (value == null)
                    valueDisambiguation = DefinitionSet.DisambiguationNull;
                else if (value.Length < 1)
                    valueDisambiguation = DefinitionSet.DisambiguationEmpty;
                Options.Items.Add(string.Format(DefinitionSet.FmtValue, GenerateDoubleName<VALUES>(item), value, valueDisambiguation));
            } //loop
            Options.Items.Add(string.Empty);
            Options.Items.Add(DefinitionSet.SectionFiles);
            foreach(string value in commandLine.Files)
                Options.Items.Add(string.Format(DefinitionSet.FmtStringProperty, value));
            Errors.Items.Add(string.Empty);
            Errors.Items.Add(DefinitionSet.SectionUnrecognized);
            foreach (string value in commandLine.UnrecognizedOptions)
                Errors.Items.Add(string.Format(DefinitionSet.FmtStringProperty, value));
            Errors.Items.Add(string.Empty);
            Errors.Items.Add(DefinitionSet.SectionRepeatedSwitches);
            foreach (string value in commandLine.RepeatedSwitches)
                Errors.Items.Add(string.Format(DefinitionSet.FmtStringProperty, value));
            Errors.Items.Add(string.Empty);
            Errors.Items.Add(DefinitionSet.SectionRepeatedValues);
            foreach (string value in commandLine.RepeatedValues)
                Errors.Items.Add(string.Format(DefinitionSet.FmtStringProperty, value));
            Errors.Items.Add(string.Empty);
            Errors.Items.Add(DefinitionSet .SectionRepeatedFiles);
            foreach (string value in commandLine.RepeatedFiles)
                Errors.Items.Add(string.Format(DefinitionSet.FmtStringProperty, value));
        } //Show

        static string GenerateDoubleName<ENUM>(EnumerationItem<ENUM> item) {
            string name = item.Name;
            string abbreviation = item.AbbreviatedName;
            if (name == abbreviation)
                return name;
            else
                return string.Format(DefinitionSet.FmtDoubleName, name, abbreviation);
        } //GenerateDoubleName

        ListBox Options, Errors;

    } //CommandLineReport

} //namespace CommandLineTest

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Physics, physical and quantum optics, mathematics, computer science, control systems for manufacturing, diagnostics, testing, and research, theory of music, musical instruments… Contact me: https://www.SAKryukov.org

Comments and Discussions