Java tutorial
/* * * Copyright 2018 FJN Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Author Date Issue * fs1194361820@163.com 2015-01-01 Initial Version * */ package com.fjn.helper.frameworkex.apache.commons.cli; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.junit.BeforeClass; import org.junit.Test; /** * Usage: ls [OPTION]... [FILE]...<br> * List information about the FILEs (the current directory by default).<br> * Sort entries alphabetically if none of -cftuSUX nor --sort.<br> * <br> * -a, --all do not hide entries starting with .<br> * -A, --almost-all do not list implied . and ..<br> * -b, --escape print octal escapes for nongraphic characters<br> * --block-size=SIZE use SIZE-byte blocks<br> * -B, --ignore-backups do not list implied entries ending with ~<br> * -c with -lt: sort by, and show, ctime (time of last modification of file * status information) with -l: show ctime and sort by name otherwise: sort by * ctime -C list entries by columns<br> * * @author fs1194361820@163.com * */ public class Ls { static Options options = new Options(); @BeforeClass public static void initOptions() { options.addOption("a", "all", false, "do not hide entries starting with ."); options.addOption(new Option("A", "almost-all", false, "do not list implied . and ..")); options.addOption("b", "escape", false, "print octal escapes for nongraphic " + "characters"); options.addOption(Option.builder("").longOpt("block-size").desc("use SIZE-byte blocks").hasArg() .argName("SIZE").build()); options.addOption("B", "ignore-backups", false, "do not list implied entried " + "ending with ~"); options.addOption("c", false, "with -lt: sort by, and show, ctime (time of last " + "modification of file status information) with " + "-l:show ctime and sort by name otherwise: sort " + "by ctime"); options.addOption("C", false, "list entries by columns"); } @Test public void test() throws ParseException { CommandLineParser parser = new DefaultParser(); String[] args = new String[] { "--block-size=10" }; CommandLine line = parser.parse(options, args); // validate that block-size has been set if (line.hasOption("block-size")) { // print the value of block-size System.out.println(line.getOptionValue("block-size")); } } }