예제 #1
0
        /// <summary>
        /// Process the command line arguments against the
        /// expected options.
        /// </summary>
        /// <param name="args">
        /// The command line arguments.
        /// </param>
        /// <returns>
        /// A CommandLine object if successful, otherwise false.
        /// </returns>
        private static CommandLine ProcessArguments(string[] args)
        {
            var         pp = new PosixParser();
            CommandLine cl;

            try
            {
                cl = pp.Parse(
                    Opts,
                    args);
            }
            catch (ParseException e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }

            if (cl.HasOption("?"))
            {
                return(null);
            }

            if (!(cl.HasOption("sf") ||
                  cl.HasOption("pf") ||
                  cl.HasOption("d") ||
                  cl.HasOption("f")))
            {
                return(null);
            }

            return(cl);
        }
예제 #2
0
        /// <summary>Main entry point for ToolRunner (see ToolRunner docs)</summary>
        /// <param name="argv">The parameters passed to this program.</param>
        /// <returns>0 on success, non zero on error.</returns>
        /// <exception cref="System.Exception"/>
        public virtual int Run(string[] argv)
        {
            Options options = BuildOptions();

            if (argv.Length == 0)
            {
                PrintHelp();
                return(-1);
            }
            CommandLineParser parser = new PosixParser();
            CommandLine       cmd;

            try
            {
                cmd = parser.Parse(options, argv);
            }
            catch (ParseException e)
            {
                System.Console.Out.WriteLine("Error parsing command-line options: " + e.Message);
                PrintHelp();
                return(-1);
            }
            if (cmd.HasOption("h"))
            {
                // print help and exit
                PrintHelp();
                return(-1);
            }
            string inputFileName  = cmd.GetOptionValue("i");
            string outputFileName = cmd.GetOptionValue("o");
            string processor      = cmd.GetOptionValue("p");

            if (processor == null)
            {
                processor = defaultProcessor;
            }
            OfflineEditsViewer.Flags flags = new OfflineEditsViewer.Flags();
            if (cmd.HasOption("r"))
            {
                flags.SetRecoveryMode();
            }
            if (cmd.HasOption("f"))
            {
                flags.SetFixTxIds();
            }
            if (cmd.HasOption("v"))
            {
                flags.SetPrintToScreen();
            }
            return(Go(inputFileName, outputFileName, processor, flags, null));
        }
예제 #3
0
            /// <exception cref="Org.Apache.Commons.Cli.ParseException"/>
            public virtual void Parse(params string[] argv)
            {
                CommandLineParser parser  = new PosixParser();
                CommandLine       cmdLine = parser.Parse(options, argv);

                if (cmdLine.HasOption(helpOpt.GetOpt()) || cmdLine.HasOption(helpOpt.GetLongOpt()
                                                                             ))
                {
                    shouldPrintHelp = true;
                    return;
                }
                bool hasGetEdit    = cmdLine.HasOption(geteditsizeOpt.GetOpt());
                bool hasCheckpoint = cmdLine.HasOption(checkpointOpt.GetOpt());

                if (hasGetEdit && hasCheckpoint)
                {
                    throw new ParseException("May not pass both " + geteditsizeOpt.GetOpt() + " and "
                                             + checkpointOpt.GetOpt());
                }
                if (hasGetEdit)
                {
                    cmd = SecondaryNameNode.CommandLineOpts.Command.Geteditsize;
                }
                else
                {
                    if (hasCheckpoint)
                    {
                        cmd = SecondaryNameNode.CommandLineOpts.Command.Checkpoint;
                        string arg = cmdLine.GetOptionValue(checkpointOpt.GetOpt());
                        if ("force".Equals(arg))
                        {
                            shouldForce = true;
                        }
                        else
                        {
                            if (arg != null)
                            {
                                throw new ParseException("-checkpoint may only take 'force' as an " + "argument");
                            }
                        }
                    }
                }
                if (cmdLine.HasOption(formatOpt.GetOpt()))
                {
                    shouldFormat = true;
                }
            }
예제 #4
0
 /// <summary>Parses the command line options</summary>
 /// <returns>false if need to print help output</returns>
 /// <exception cref="System.Exception">when parsing fails</exception>
 internal virtual ArgumentParser.ParsedOutput Parse()
 {
     if (parsed == null)
     {
         PosixParser parser = new PosixParser();
         CommandLine popts  = parser.Parse(GetOptionList(), argumentList, true);
         if (popts.HasOption(ConfigOption.Help.GetOpt()))
         {
             parsed = new ArgumentParser.ParsedOutput(null, this, true);
         }
         else
         {
             parsed = new ArgumentParser.ParsedOutput(popts, this, false);
         }
     }
     return(parsed);
 }
예제 #5
0
        static int Main(string[] args)
        {
            try
            {
                Toolchain.Initialize();

                ServicePointManager.ServerCertificateValidationCallback
                    = (o, cert, chain, err) => true;

                var options = PosixParser.ParseArgs <ProgramOptions>(args: args, lenient: true);

                if (options.Help.On)
                {
                    PosixPrinter.PrintHelp(options);
                    return(0);
                }

                if (options.Command.On)
                {
                    var commandName = options.Command.Text;
                    var commandArgs = args.Skip(1).ToArray();
                    return(CallCommand(commandName, commandArgs));
                }

                Prompt.PrintInvalidUsage("Nenhum comando foi indicado na linha de comando.");
                return(1);
            }
            catch (Exception ex) when(ex is InvalidUsageException || ex?.InnerException is InvalidUsageException)
            {
                ex = (ex as InvalidUsageException) ?? (ex.InnerException as InvalidUsageException);
                Prompt.PrintInvalidUsage(ex.Message);
                return(1);
            }
            catch (Exception ex) when(ex is PromptException || ex?.InnerException is PromptException)
            {
                ex = (ex as PromptException) ?? (ex.InnerException as PromptException);
                Prompt.PrintFault(ex.Message);
                return(1);
            }
            catch (Exception ex)
            {
                Prompt.PrintFault(ex, null);
                return(1);
            }
        }
예제 #6
0
        private static int CallCommand(string commandName, string[] commandArgs)
        {
            var commands = new CommandCatalog();
            var command  = (
                from x in commands
                let name = CommandCatalog.GetCommandName(x)
                           where name == commandName
                           select x
                ).FirstOrDefault();

            if (command == null)
            {
                Prompt.PrintInvalidUsage($"Comando não encontrado: {commandName}");
                return(1);
            }

            var commandMethod = CommandCatalog.GetCommandMethod(command);

            var commmandOptionsType = CommandCatalog.GetOptionsType(command);
            var commmandOptions     = Activator.CreateInstance(commmandOptionsType);

            PosixParser.ParseArgs(commmandOptions, commandArgs);

            var result = commandMethod.Invoke(command, new[] { commmandOptions });

            if (result is int)
            {
                return((int)result);
            }

            if (result is bool)
            {
                return(true.Equals(result) ? 0 : 1);
            }

            return(0);
        }
예제 #7
0
        /// <exception cref="System.Exception"/>
        public static int Run(string[] args)
        {
            Options options = BuildOptions();

            if (args.Length == 0)
            {
                PrintUsage();
                return(0);
            }
            CommandLineParser parser = new PosixParser();
            CommandLine       cmd;

            try
            {
                cmd = parser.Parse(options, args);
            }
            catch (ParseException)
            {
                System.Console.Out.WriteLine("Error parsing command-line options: ");
                PrintUsage();
                return(-1);
            }
            if (cmd.HasOption("h"))
            {
                // print help and exit
                PrintUsage();
                return(0);
            }
            string inputFile  = cmd.GetOptionValue("i");
            string processor  = cmd.GetOptionValue("p", "Web");
            string outputFile = cmd.GetOptionValue("o", "-");
            string delimiter  = cmd.GetOptionValue("delimiter", PBImageDelimitedTextWriter.DefaultDelimiter
                                                   );
            string        tempPath = cmd.GetOptionValue("t", string.Empty);
            Configuration conf     = new Configuration();

            try
            {
                using (TextWriter @out = outputFile.Equals("-") ? System.Console.Out : new TextWriter
                                             (outputFile, "UTF-8"))
                {
                    switch (processor)
                    {
                    case "FileDistribution":
                    {
                        long maxSize = long.Parse(cmd.GetOptionValue("maxSize", "0"));
                        int  step    = System.Convert.ToInt32(cmd.GetOptionValue("step", "0"));
                        new FileDistributionCalculator(conf, maxSize, step, @out).Visit(new RandomAccessFile
                                                                                            (inputFile, "r"));
                        break;
                    }

                    case "XML":
                    {
                        new PBImageXmlWriter(conf, @out).Visit(new RandomAccessFile(inputFile, "r"));
                        break;
                    }

                    case "Web":
                    {
                        string addr = cmd.GetOptionValue("addr", "localhost:5978");
                        using (WebImageViewer viewer = new WebImageViewer(NetUtils.CreateSocketAddr(addr)
                                                                          ))
                        {
                            viewer.Start(inputFile);
                        }
                        break;
                    }

                    case "Delimited":
                    {
                        using (PBImageDelimitedTextWriter writer = new PBImageDelimitedTextWriter(@out, delimiter
                                                                                                  , tempPath))
                        {
                            writer.Visit(new RandomAccessFile(inputFile, "r"));
                        }
                        break;
                    }
                    }
                    return(0);
                }
            }
            catch (EOFException)
            {
                System.Console.Error.WriteLine("Input file ended unexpectedly. Exiting");
            }
            catch (IOException e)
            {
                System.Console.Error.WriteLine("Encountered exception.  Exiting: " + e.Message);
            }
            return(-1);
        }
예제 #8
0
        static void Main(string[] args)
        {
            //Handles early exits
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
            //Loads embedded DLLs into EXE if they don't load automatically
            AppDomain.CurrentDomain.AssemblyResolve += (sender, arguments) => {
                string resourceName = "AssemblyLoadingAndReflection." +
                                      new AssemblyName(arguments.Name).Name + ".dll";
                using (var stream = Assembly.GetExecutingAssembly()
                                    .GetManifestResourceStream(resourceName)) {
                    byte[] assemblyData = new byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return(Assembly.Load(assemblyData));
                }
            };

            //Application initialization

            webStringUtils = new WebStringUtils(getAppFolder());
            objSaveUtils   = new ObjSaveUtils(getAppFolder() + '/');
            //Help
            //Options options = new Options();
            Options options = new Options();

            options.AddOption(new Option("h", "help", false, "display this help dialog"));
            options.AddOption(new Option("ph", "pattern help", false, "help with the -p command"));
            options.AddOption(new Option("v", "verbose", false, "verbose mode"));
            //options.AddOptionGroup(helpOptions);
            //Crawl options
            options.AddOption(new Option("dc", "dont crawl", false, "do not execute crawl. (for the purpose of using other utilities only)"));
            options.AddOption(new Option("vi", "visited", false, "print visited pages a after completion (n.i.)"));
            options.AddOption(new Option("ul", "unlock", false, "unlocks crawler from target domain"));
            options.AddOption(new Option("bc", "backcrawl", false, "deep copy, enables discovery of hidden pages (slow)"));
            options.AddOption(new Option("p", "pattern", true, "regex pattern for restricting pages"));
            options.AddOption(new Option("d", "depth", true, "depth of the search (default 10)"));
            options.AddOption(new Option("c", "courtesy", true, "delay between page loads, in milliseconds"));
            options.AddOption(new Option("t", "threads", true, "number of allowed threads. More threads = more aggressive (must be 2+)"));
            options.AddOption(new Option("i", "iterative", true, "scans urls iteratively in the form of url/1,2.. starting at <param>"));
            options.AddOption(new Option("id", "iterative depth", true, "Depth to scan to at each step of the iteration"));
            //File options
            options.AddOption(new Option("O", "overwrite", false, "overwrite files when scan starts"));
            Option downloadImages = new Option("di", "images", true, "download images while crawling (takes regex for filtering)");

            downloadImages.OptionalArg  = true;
            downloadImages.NumberOfArgs = 1;
            options.AddOption(downloadImages);
            Option downloadText = new Option("dt", "text", false, "download text bodies for analyzation <tag, regex>");

            downloadText.OptionalArg    = true;
            downloadText.NumberOfArgs   = 2;
            downloadText.ValueSeparator = ' ';
            options.AddOption(downloadText);
            options.AddOption(new Option("il", "include link", false, "include links to the parent page in text files"));
            options.AddOption(new Option("g", "gallery", false, "only download files to one folder"));
            options.AddOption(new Option("o", "output", true, "output location (defaults to exe location)"));
            options.AddOption(new Option("l", "load", true, "load data from previous scan, named <param>"));
            //Database options
            options.AddOption(new Option("dbl", "database links", false, "Save visited links into the DB, with tags defined by -dt"));
            options.AddOption(new Option("dbi", "database images", false, "Save image locations to database, with tags defined by -dt"));
            options.AddOption(new Option("ddh", "HTML", false, "don't download HTML while crawling"));
            options.AddOption(new Option("dbc", "database check", false, "Check the database to prevent duplicate entries (slow)"));
            options.AddOption(new Option("dbip", "database ip", true, "the IP address of the database to dump to"));
            //Data processing
            options.AddOption(new Option("m", "markov", true, "generate a markov chain of <param> prefix Length and saves it."));
            options.AddOption(new Option("mp", "print markov", true, "prints out [param] sentences from the chain (Must use -g)"));
            //Attempts to parse args
            try {
                ICommandLineParser parser = new PosixParser();
                //Help options
                CommandLine   helpCmd       = parser.Parse(options, args);
                HelpFormatter helpFormatter = new HelpFormatter();
                helpFormatter.Width       = 100;
                helpFormatter.DescPadding = 0x1;
                //string helpHeader = "\nSKS Web crawler/info extractor v0.1";
                string helpHeader = "\nSKS Web crawler/info extractor v0.1";
                string helpFooter = "\nExample Usage: java -jar [JARNAME] http://pornhub.com -di -d 5"
                                    + "\nSite Image Gallery: [URL] -di -ddh -g"
                                    + "\nFullsize gallery of 4chan thread: [URL] -di ^((?!s.).)*$ -ddh -g -p .*/((?!#[spq]).)*"
                                    + "\nSankaku tags on posts with urls: [URL] -g -il -ddh -dt title (.*)(/post/show/)(.*) -O -c 1000 -d 3"
                                    + "\nIterative booru tag crawl: [BASEURL] -g -il -ddh -dt title -O -c 1000 -d 1000 -i <startpage>"
                                    + "\nMarkov chain from 4chan board: [URL] -t 10 -d 15 -dt .post .* -m 2 -g -ddh -O -mp 40"
                                    + "\nInsert images into database with tags: [BOORUURL] -g -t 10 -di .*[/](_images/).* -ddh -d 10 -O -p .*[/]post/.* -ul -dt title -dbi";
                if (helpCmd.HasOption("ph"))
                {
                    Console.WriteLine("\n-p and -i take a regular exp. as an argument, searching all URLs"
                                      + "\nthat match the pattern. I.E., \"test.com/page \" would "
                                      + "\nmatch \"test.com/page/page2\". To test for any subdomain,"
                                      + "\nthe following pattern would operate on [anything].test.com:"
                                      + "\nhttps?://([^/.]+[.])*test.com(.*)");
                    return;
                }
                data.verbose = helpCmd.HasOption("v");
                //Crawl options
                CommandLine crawlCmd = parser.Parse(options, args);

                if (args.Length > 0)
                {
                    data.startURL = args[0];
                }
                data.backCrawl      = crawlCmd.HasOption("bc");
                data.iterative      = crawlCmd.HasOption("i");
                shouldCrawl         = !crawlCmd.HasOption("dc");
                data.iteratorStart  = Convert.ToInt32(crawlCmd.GetOptionValue("i", "0"));
                data.iterativeDepth = Convert.ToInt32(crawlCmd.GetOptionValue("id", "0"));
                data.crawlPattern   = crawlCmd.GetOptionValue("p", ".*");
                data.maxDepth       = Convert.ToInt32(crawlCmd.GetOptionValue("d", "5"));
                data.delay          = Convert.ToInt32(crawlCmd.GetOptionValue("c", "0"));
                crawlThreadExecutor = new LimitedConcurrencyLevelTaskScheduler(Convert.ToInt32(crawlCmd.GetOptionValue("t", "2")));
                crawlThreadFactory  = new TaskFactory(crawlThreadExecutor);
                crawlLocked         = !crawlCmd.HasOption("ul");

                //File options
                CommandLine fileCmd = parser.Parse(options, args);
                data.overwrite      = fileCmd.HasOption("O");
                data.downloadImages = fileCmd.HasOption("di");
                data.imagePattern   = fileCmd.GetOptionValue("di", "");
                data.downloadText   = fileCmd.HasOption("dt");
                data.downloadHTML   = !fileCmd.HasOption("ddh");
                data.gallery        = fileCmd.HasOption("g");

                if (data.downloadText)
                {
                    string[] imageOptions = fileCmd.GetOptionValues("dt");
                    //textTag = cmd.GetOptionValue("dt", null);
                    data.textTag = imageOptions[0];
                    try {
                        data.textPattern = imageOptions[1];
                    } catch (Exception) {
                        data.textPattern = "";
                    }
                    data.includeLinks = fileCmd.HasOption("il");
                }
                if (fileCmd.HasOption("l"))
                {
                    saveFile = fileCmd.GetOptionValue("l");
                    //Loads the chain
                    if (fileCmd.HasOption("m"))
                    {
                        markovChain = (MarkovChain)objSaveUtils.LoadObject("markov_" + saveFile, typeof(MarkovChain));
                    }
                    //Loads the tries
                    data.urlTrie    = (WebTrie)objSaveUtils.LoadObject("visitedTrie_" + saveFile, typeof(WebTrie));
                    data.assistTrie = (WebTrie)objSaveUtils.LoadObject("assistTrie_" + saveFile, typeof(WebTrie));
                    data.mediaTrie  = (WebTrie)objSaveUtils.LoadObject("assistTrie_" + saveFile, typeof(WebTrie));
                }
                else
                {
                    if (args.Length > 0)
                    {
                        saveFile = webStringUtils.UnFuck(args[0]);
                    }
                    //If not loading chain from file, create new chain
                    if (fileCmd.HasOption("m"))
                    {
                        markovChain = new MarkovChain(Convert.ToInt32(fileCmd.GetOptionValue("m", "3")));
                    }
                    //Attempts to automatically load file name
                    try {
                        data.urlTrie    = (WebTrie)objSaveUtils.LoadObject("visitedTrie_" + saveFile, typeof(WebTrie));
                        data.assistTrie = (WebTrie)objSaveUtils.LoadObject("assistTrie_" + saveFile, typeof(WebTrie));
                        data.mediaTrie  = (WebTrie)objSaveUtils.LoadObject("assistTrie_" + saveFile, typeof(WebTrie));
                    } catch (Exception) {
                        //Generate tries if not loadable
                        data.urlTrie    = new WebTrie();
                        data.assistTrie = new WebTrie();
                        data.mediaTrie  = new WebTrie();
                    }
                }
                data.outputFolder = fileCmd.GetOptionValue("o", getAppFolder()) + "CrawlResults\\";

                //Database options
                CommandLine dbCmd = parser.Parse(options, args);
                if (dbCmd.HasOption("dbip"))
                {
                    TagDBDriver.instantiateDB(dbCmd.GetOptionValue("dbip"));
                    data.dataBaseImages = dbCmd.HasOption("dbi");
                    data.dataBaseLinks  = dbCmd.HasOption("dbl");
                    data.dataBaseCheck  = dbCmd.HasOption("dbc");
                }

                //Data processing options
                CommandLine dpCmd = parser.Parse(options, args);
                printMarkov     = dpCmd.HasOption("mp");
                markovSentences = Convert.ToInt32(dpCmd.GetOptionValue("mp", "0"));

                if (helpCmd.HasOption("h") || args.Length == 0)
                {
                    printHelp();
                    return;
                }
            } catch (Exception exception) {
                Console.WriteLine("Invalid arguments or parameters. use -h for help (" + exception + ")");
                return;
            }
            //instantiates trie

            //creates regex for site locking
            if (crawlLocked)
            {
                string regexURL = Regex.Replace(args[0], "https?://", "");
                data.crawlDomain = "https?://([^/.]+[.])*" + regexURL + "(.*)";
            }
            else
            {
                data.crawlDomain = ".*";
            }

            try {
                Crawl(args[0], data);
            } catch (Exception e) {
                Console.WriteLine("Scan aborted: " + e);
            }
            // System.exit(0);
        }
예제 #9
0
        /// <summary>
        /// Parse the given argument vector
        /// </summary>
        /// <param name="argv">The arguments</param>
        /// <returns>The Command instance</returns>
        public Command Parse(string[] argv) 
        {
            PosixParser parser = new PosixParser();

            CommandLine cmdline = null;
            cmdline = parser.Parse(this.rules, argv);

            // Unpack the cmdline into a simple Map of options and optionally
            // assign values to any corresponding fields found in the Command 
            // class.
            foreach (Option option in cmdline.Options) 
            {
                string name = option.LongOpt;
                object value = option.Values;
                if (option.NumberOfArgs == 1)
                {
                    value = ((string[])value)[0];
                }

                // Figure out the type of the option and convert the value.
                if (!option.HasArg) 
                {
                    // If it has no arg, then its implicitly boolean and 
                    // presence of the argument indicates truth.
                    value = true;
                }
                else 
                {
                    Type type = (Type)option.Type;
                    if (type == null || type == typeof(string)) 
                    {
                        // Null implies string, check for multivalue 
                        // (unsupported)
                    } 
                    else if (type == typeof(int)) 
                    {
                        value = Convert.ToInt32((string)value);
                    }
                    else 
                    {
                        throw new Exception("Unsupported type"); 
                    }
                }

                if (this.Opts.ContainsKey(name))
                {
                    // the established value is being overwritten by a newer 
                    // value
                    this.Opts.Remove(name);
                }
                this.Opts.Add(name, value);

                // Look for a field of the Command class (or subclass) that
                // matches the long name of the option and, if found, assign the
                // corresponding option value in order to provide simplified
                // access to command options.
                FieldInfo field = this.GetType().GetField(name);
                if (field != null)
                {
                    field.SetValue(this, value);
                }
            }

            string[] orig = this.args;
            string[] more = cmdline.Args;
            this.args = new string[orig.Length + cmdline.Args.Length];

            orig.CopyTo(this.args, 0);
            cmdline.Args.CopyTo(this.args, orig.Length);

            if (this.Help) 
            {
                this.PrintHelp();
                Environment.Exit(0);
            }

            return this;
        }
예제 #10
0
        static void Main(string[] arguments)
        {
            ICommandLineParser commandLineParser = new PosixParser();
            CommandLine        commandLine       = commandLineParser.Parse(Options, arguments);

            int port = SocketOpenOfficeConnection.DefaultPort;

            if (commandLine.HasOption(OptionPort.Opt))
            {
                port = Convert.ToInt32(commandLine.GetOptionValue(OptionPort.Opt));
            }

            String outputFormat = null;

            if (commandLine.HasOption(OptionOutputFormat.Opt))
            {
                outputFormat = commandLine.GetOptionValue(OptionOutputFormat.Opt);
            }

            bool verbose = commandLine.HasOption(OptionVerbose.Opt);

            IDocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();

            String[] fileNames = commandLine.Args;
            if ((outputFormat == null && fileNames.Length != 2) || fileNames.Length < 1)
            {
                String        syntax        = "Convert [options] input-file output-file; or\n" + "[options] -f output-format input-file [input-file...]";
                HelpFormatter helpFormatter = new HelpFormatter();
                helpFormatter.PrintHelp(syntax, Options);
                Environment.Exit(ExitCodeTooFewArgs);
            }

            IOpenOfficeConnection connection = new SocketOpenOfficeConnection(port);
            OfficeInfo            oo         = EnvUtils.Get();

            if (oo.Kind == OfficeKind.Unknown)
            {
                Console.Out.WriteLine("please setup OpenOffice or LibreOffice!");
                return;
            }
            try
            {
                if (verbose)
                {
                    Console.Out.WriteLine("-- connecting to OpenOffice.org on port " + port);
                }
                connection.Connect();
            }
            catch (Exception)
            {
                string CmdArguments = string.Format("-headless -accept=\"socket,host={0},port={1};urp;\" -nofirststartwizard", SocketOpenOfficeConnection.DefaultHost, SocketOpenOfficeConnection.DefaultPort);
                if (!EnvUtils.RunCmd(oo.OfficeUnoPath, "soffice", CmdArguments))
                {
                    Console.Error.WriteLine("ERROR: connection failed. Please make sure OpenOffice.org is running and listening on port " + port + ".");
                    Environment.Exit(ExitCodeConnectionFailed);
                }
            }
            try
            {
                IDocumentConverter converter = new OpenOfficeDocumentConverter(connection, registry);
                if (outputFormat == null)
                {
                    FileInfo inputFile  = new FileInfo(fileNames[0]);
                    FileInfo outputFile = new FileInfo(fileNames[1]);
                    ConvertOne(converter, inputFile, outputFile, verbose);
                }
                else
                {
                    foreach (var t in fileNames)
                    {
                        var inputFile  = new FileInfo(t);
                        var outputFile = new FileInfo(inputFile.FullName.Remove(inputFile.FullName.LastIndexOf(".", StringComparison.Ordinal)) + "." + outputFormat);
                        ConvertOne(converter, inputFile, outputFile, verbose);
                    }
                }
            }
            finally
            {
                if (verbose)
                {
                    Console.Out.WriteLine("-- disconnecting");
                }
                connection.Disconnect();
            }
        }
예제 #11
0
        /// <summary>
        /// Process the command line arguments against the
        /// expected options.
        /// </summary>
        /// <param name="args">
        /// The command line arguments.
        /// </param>
        /// <returns>
        /// A CommandLine object if successful, otherwise false.
        /// </returns>
        private static CommandLine ProcessArguments(string[] args)
        {
            var pp = new PosixParser();
            CommandLine cl;

            try
            {
                cl = pp.Parse(
                    Opts,
                    args);
            }
            catch (ParseException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }

            if (cl.HasOption("?"))
            {
                return null;
            }

            if (!(cl.HasOption("sf") ||
                  cl.HasOption("pf") ||
                  cl.HasOption("d") ||
                  cl.HasOption("f")))
            {
                return null;
            }

            return cl;
        }
예제 #12
0
        /// <summary>Entry point to command-line-driven operation.</summary>
        /// <remarks>
        /// Entry point to command-line-driven operation.  User may specify
        /// options and start fsimage viewer from the command line.  Program
        /// will process image file and exit cleanly or, if an error is
        /// encountered, inform user and exit.
        /// </remarks>
        /// <param name="args">Command line options</param>
        /// <exception cref="System.IO.IOException"></exception>
        public static void Main(string[] args)
        {
            Options options = BuildOptions();

            if (args.Length == 0)
            {
                PrintUsage();
                return;
            }
            CommandLineParser parser = new PosixParser();
            CommandLine       cmd;

            try
            {
                cmd = parser.Parse(options, args);
            }
            catch (ParseException)
            {
                System.Console.Out.WriteLine("Error parsing command-line options: ");
                PrintUsage();
                return;
            }
            if (cmd.HasOption("h"))
            {
                // print help and exit
                PrintUsage();
                return;
            }
            bool   skipBlocks    = cmd.HasOption("skipBlocks");
            bool   printToScreen = cmd.HasOption("printToScreen");
            string inputFile     = cmd.GetOptionValue("i");
            string processor     = cmd.GetOptionValue("p", "Ls");
            string outputFile    = cmd.GetOptionValue("o");
            string delimiter     = cmd.GetOptionValue("delimiter");

            if (!(delimiter == null || processor.Equals("Delimited")))
            {
                System.Console.Out.WriteLine("Can only specify -delimiter with Delimited processor"
                                             );
                PrintUsage();
                return;
            }
            ImageVisitor v;

            if (processor.Equals("Indented"))
            {
                v = new IndentedImageVisitor(outputFile, printToScreen);
            }
            else
            {
                if (processor.Equals("XML"))
                {
                    v = new XmlImageVisitor(outputFile, printToScreen);
                }
                else
                {
                    if (processor.Equals("Delimited"))
                    {
                        v = delimiter == null ? new DelimitedImageVisitor(outputFile, printToScreen) : new
                            DelimitedImageVisitor(outputFile, printToScreen, delimiter);
                        skipBlocks = false;
                    }
                    else
                    {
                        if (processor.Equals("FileDistribution"))
                        {
                            long maxSize = long.Parse(cmd.GetOptionValue("maxSize", "0"));
                            int  step    = System.Convert.ToInt32(cmd.GetOptionValue("step", "0"));
                            v = new FileDistributionVisitor(outputFile, maxSize, step);
                        }
                        else
                        {
                            if (processor.Equals("NameDistribution"))
                            {
                                v = new NameDistributionVisitor(outputFile, printToScreen);
                            }
                            else
                            {
                                v          = new LsImageVisitor(outputFile, printToScreen);
                                skipBlocks = false;
                            }
                        }
                    }
                }
            }
            try
            {
                Org.Apache.Hadoop.Hdfs.Tools.OfflineImageViewer.OfflineImageViewer d = new Org.Apache.Hadoop.Hdfs.Tools.OfflineImageViewer.OfflineImageViewer
                                                                                           (inputFile, v, skipBlocks);
                d.Go();
            }
            catch (EOFException)
            {
                System.Console.Error.WriteLine("Input file ended unexpectedly.  Exiting");
            }
            catch (IOException e)
            {
                System.Console.Error.WriteLine("Encountered exception.  Exiting: " + e.Message);
            }
        }
예제 #13
0
        /// <summary>
        /// Parse the given argument vector
        /// </summary>
        /// <param name="argv">The arguments</param>
        /// <returns>The Command instance</returns>
        public Command Parse(string[] argv)
        {
            PosixParser parser = new PosixParser();

            CommandLine cmdline = null;

            cmdline = parser.Parse(this.rules, argv);

            // Unpack the cmdline into a simple Map of options and optionally
            // assign values to any corresponding fields found in the Command
            // class.
            foreach (Option option in cmdline.Options)
            {
                string name  = option.LongOpt;
                object value = option.Values;
                if (option.NumberOfArgs == 1)
                {
                    value = ((string[])value)[0];
                }

                // Figure out the type of the option and convert the value.
                if (!option.HasArg)
                {
                    // If it has no arg, then its implicitly boolean and
                    // presence of the argument indicates truth.
                    value = true;
                }
                else
                {
                    Type type = (Type)option.Type;
                    if (type == null || type == typeof(string))
                    {
                        // Null implies string, check for multivalue
                        // (unsupported)
                    }
                    else if (type == typeof(int))
                    {
                        value = Convert.ToInt32((string)value);
                    }
                    else
                    {
                        throw new Exception("Unsupported type");
                    }
                }

                if (this.Opts.ContainsKey(name))
                {
                    // the established value is being overwritten by a newer
                    // value
                    this.Opts.Remove(name);
                }
                this.Opts.Add(name, value);

                // Look for a field of the Command class (or subclass) that
                // matches the long name of the option and, if found, assign the
                // corresponding option value in order to provide simplified
                // access to command options.
                FieldInfo field = this.GetType().GetField(name);
                if (field != null)
                {
                    field.SetValue(this, value);
                }
            }

            string[] orig = this.args;
            string[] more = cmdline.Args;
            this.args = new string[orig.Length + cmdline.Args.Length];

            orig.CopyTo(this.args, 0);
            cmdline.Args.CopyTo(this.args, orig.Length);

            if (this.Help)
            {
                this.PrintHelp();
                Environment.Exit(0);
            }

            return(this);
        }