예제 #1
0
        }//end constructor

        /// <summary>
        /// Add an Argument for the parser to look for
        /// </summary>
        /// <param name="sname">Short name of the argument</param>
        /// <param name="lname">Long name of the argument</param>
        /// <param name="has_opt">True if there is an option to follow the argument, i.e.: -a 'argument' </param>
        /// <param name="help">Help string to display when help is called</param>
        /// <returns>CmdLineArg object that can be queried after the command line options are parsed</returns>
        public CmdLineArg AddArgument(string sname, string lname, bool has_opt, string help)
        {
            CmdLineArg arg = new CmdLineArg(sname, lname, has_opt, help);


            if (m_arguments.ContainsKey(sname))
            {
                Console.WriteLine("Argument name: " + sname + " is already in use. Disregarding name.");
                return(null);
            }

            if (m_arguments.ContainsKey(lname))
            {
                Console.WriteLine("Argument name: " + lname + " is already in use. Disregarding name.");
                return(null);
            }

            //add argument names to the dictionary
            m_arguments.Add(sname, arg);
            m_arguments.Add(lname, arg);

            //add argument object to the list
            m_argument_objs.Add(arg);

            return(arg);
        }
예제 #2
0
        /// <summary>
        /// Parse the command line arguments
        /// </summary>
        /// <param name="args"></param>
        public bool parseArgs(string[] args)
        {
            int arg_length = args.Length;
            int i          = 0;

            while (i < arg_length)
            {
                //get the arguemnt and clean it up
                string arg = args[i];
                arg = arg.Trim();

                if (arg.Length >= 2)
                {
                    string head = arg.Substring(0, 1);
                    if (head == "-")
                    {
                        arg = arg.Remove(0, 1);
                    }
                    else
                    {
                        //malformed option....
                        parseError("Malformed argument: " + arg);
                        return(false);
                    }

                    if (argExists(arg))
                    {
                        CmdLineArg cla = m_arguments[arg];

                        if (cla.Present)
                        {
                            //error - duplicate argument
                            parseError("Duplicate argument: " + arg);
                            return(false);
                        }
                        else
                        {
                            //flag as present
                            cla.Present = true;

                            //check for option
                            if (cla.HasOption)
                            {
                                int next = i + 1;
                                if (next < arg_length)
                                {
                                    string arg_opt = args[next];
                                    cla.Option = arg_opt;

                                    //incemrent i for option
                                    i += 1;
                                }
                                else
                                {
                                    //error - out or arguments...
                                    parseError("Missing argument option for: " + arg);
                                    return(false);
                                }
                            }
                        }
                    }
                    else
                    {
                        //argument not recognized
                        parseError("Argument not recognized: " + arg);
                        return(false);
                    }
                }
                else
                {
                    //malformed, needs to be at least "-a"
                    parseError("Malformed argument: " + arg);
                    return(false);
                }

                //increment i for argument
                //i is also incremented above for options
                i += 1;
            }//end while

            return(true);
        }//end parse arguments
        /// <summary>
        /// Run the Program
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public void run(string[] args)
        {
            ArgumentParser parser = new ArgumentParser();

            string local_path = Environment.CurrentDirectory;

            Console.WriteLine("<<< Javascript File Manager Utility >>>");



            //init the arguments
            CmdLineArg arg_help = parser.AddArgument("h", "help", false, "Displays the program arguments");
            CmdLineArg arg_dir  = parser.AddArgument("d", "directory", true, "Sets the working directory, current program directory is used if omitted.");

            CmdLineArg arg_manifest = parser.AddArgument("m", "manifest", false, "Generate manifest file.  The manifest can be edited for compilation order.  This argument must be used alone, with no other arguments");

            CmdLineArg arg_compile     = parser.AddArgument("c", "compile", false, "Compile all the javascript files into one text file.  Uses manifest if present.");
            CmdLineArg arg_compile_min = parser.AddArgument("min", "minimum", false, "Compile option argument to a minimum compilation by removing comments and line breaks.");
            CmdLineArg arg_links       = parser.AddArgument("l", "links", false, "Generate html link tags for all the javascript files.  Uses manifest if present.");

            CmdLineArg arg_ignore = parser.AddArgument("i", "ignore", false, "Ignore manifest file.  Ignores the manifest file if present.  THe program will use all the files it discovers.");

            //check for arguments after
            //the command line args are intialized so they display
            int al = args.Length;

            //if no arguments, display help and exit
            if (al == 0)
            {
                parser.displayHelp();
                holdConsole();
                return;
            }

            //if it failed to parse the arguments display help and exit
            if (!parser.parseArgs(args))
            {
                parser.displayHelp();
                holdConsole();
                return;
            }


            //check for help
            if (arg_help.Present)
            {
                parser.displayHelp();
            }

            //check for working directory
            if (arg_dir.Present)
            {
                string w_dir = arg_dir.Option;
                if (!Directory.Exists(w_dir))
                {
                    Console.WriteLine("Working directory could not be set, directory not found: ");
                    Console.WriteLine(w_dir);
                    holdConsole();
                    return;
                }

                //set the working directory
                try {
                    Directory.SetCurrentDirectory(w_dir);
                    local_path = w_dir;
                } catch (Exception ex) {
                    Console.WriteLine("An error occured setting the working directory:");
                    Console.WriteLine(w_dir);
                    Console.WriteLine("Exception: " + ex.Message);
                    holdConsole();
                    return;
                }
            }//end if directory argument


            //check for manifest flag - special case taht needs to be handled differenty
            // this is for generating the manifest only - will return early
            if (arg_manifest.Present)
            {
                //check for invalid flags - warn and exit
                if (arg_compile.Present || arg_compile_min.Present || arg_links.Present || arg_ignore.Present)
                {
                    Console.WriteLine("Error: invalid flag used with -" + arg_manifest.LongName + ", argument must be used exclusively.");
                    holdConsole();
                    return;
                }

                //create the manifest - use the filtered file list to generate
                // the manifest list - filters out generated files
                string[] m_files = getFilteredFileList();

                if (m_files.Length > 0)
                {
                    createManifest(local_path, ref m_files);
                }
                else
                {
                    Console.WriteLine("No files found to generate manifest.");
                }


                //end manifest creation
                holdConsole();
                return;
            }//end ar_manifest



            //check for -minimum flag
            if (arg_compile_min.Present && !arg_compile.Present)
            {
                Console.WriteLine("Error: -" + arg_compile_min.LongName + " argument with no -" + arg_compile.LongName + " argument");
                holdConsole();
                return;
            }



            //setup files to work with
            string[] files = null;

            //ignore manifest? - force local files with manifest flag
            if (arg_ignore.Present)
            {
                //get the local files
                Console.WriteLine("Using local files.");
                files = getFilteredFileList();
            }
            else
            {
                //try to get manifest file
                string   manifest_fn    = FILE_PREFIX + FILE_MANIFEST;
                string[] manifest_files = Directory.GetFiles(".", manifest_fn, SearchOption.TopDirectoryOnly);

                //was the file found
                if (manifest_files.Length == 0)
                {
                    Console.WriteLine("No Manifest file present.  Using local files scan.");
                    //get the local files if no manifest
                    files = getFilteredFileList();
                }
                else
                {
                    //load manifest file
                    string mpath = manifest_files[0];
                    files = getManifestList(mpath);
                    //check files
                    if (files == null)
                    {
                        Console.WriteLine("Failed to load manifest file: " + mpath);
                        holdConsole();
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Using manifest file: " + mpath);
                    }
                }
            }

            //check files
            if (files == null)
            {
                Console.WriteLine("Failed to get files to process at: " + local_path);
                holdConsole();
                return;
            }

            //number of files
            int fl = files.Length;

            //files to process?
            if (fl == 0)
            {
                Console.WriteLine("No Javascript files to process, exiting");
                holdConsole();
                return;
            }


            //generate html links?
            if (arg_links.Present)
            {
                createHTMLLinks(local_path, ref files);
            }


            //compile javascript
            if (arg_compile.Present)
            {
                if (arg_compile_min.Present)
                {
                    //minimum compile
                    compileMinimum(local_path, ref files);
                }
                else
                {
                    //standard compile
                    compile(local_path, ref files);
                }
            }


            //hold the console before exiting
            holdConsole();
        }//end run