-
-
Save toby55kij/bbcba2860ffef054277311aadc642b84 to your computer and use it in GitHub Desktop.
picocliを利用したサンプルプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.HelpCommand; | |
import picocli.CommandLine.Model.CommandSpec; | |
import picocli.CommandLine.Option; | |
import picocli.CommandLine.Parameters; | |
import picocli.CommandLine.ScopeType; | |
import picocli.CommandLine.Spec; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Consumer; | |
import java.util.stream.Stream; | |
@Command(name = "file-tool", version = "file-tool 0.1", mixinStandardHelpOptions = true, | |
description = "File tool", subcommands = HelpCommand.class, scope = ScopeType.INHERIT) | |
public class FileTool implements Runnable { | |
@Spec | |
CommandSpec commandSpec; | |
@Option(names = {"--header"}, negatable = true, | |
description = "Show header.", | |
scope = ScopeType.INHERIT) | |
boolean header; | |
@Option(names = {"-d", "--delimiter"}, defaultValue = "\t", | |
description = "Set delimiter (default: \\t).", paramLabel = "DELIMITER", | |
scope = ScopeType.INHERIT) | |
private String delimiter; | |
public static void main(String... args) { | |
int exitCode = new CommandLine(new FileTool()).execute(args); | |
System.exit(exitCode); | |
} | |
@Override | |
public void run() { | |
commandSpec.commandLine().printVersionHelp(System.out); | |
} | |
@Command(name = "size", description = "Print file size.") | |
void size(@Parameters(paramLabel = "PATH", | |
description = {"File path(s).", | |
"The default is the current directory (${DEFAULT-VALUE})."}, | |
defaultValue = "${sys:user.dir}") List<Path> paths) { | |
showHeader("size"); | |
paths.forEach(path -> walk(path.normalize().toAbsolutePath(), this::printSize)); | |
} | |
void printSize(final Path path) { | |
try { | |
print(String.valueOf(path), Long.toString(Files.size(path))); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Command(name = "type", description = "Print file content type.") | |
void contentType(@Parameters(paramLabel = "PATH", | |
description = {"File path(s).", | |
"The default is the current directory (${DEFAULT-VALUE})."}, | |
defaultValue = "${sys:user.dir}") List<Path> paths) { | |
showHeader("type"); | |
paths.forEach(path -> walk(path.normalize().toAbsolutePath(), this::printContentType)); | |
} | |
void printContentType(final Path path) { | |
try { | |
print(String.valueOf(path), Files.probeContentType(path)); | |
System.out.printf("%s%s%s%n", path, delimiter, Files.probeContentType(path)); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private void showHeader(final String... headers) { | |
if (header) { | |
print(Stream.concat(Stream.of("path"), Arrays.stream(headers)).toArray(String[]::new)); | |
} | |
} | |
private void print(final String... values) { | |
System.out.printf("%s%n", String.join(delimiter, values)); | |
} | |
private void walk(final Path path, final Consumer<Path> consumer) { | |
if (Files.isDirectory(path)) { | |
try (Stream<Path> children = Files.list(path)) { | |
children.forEachOrdered(p -> walk(p, consumer)); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} else { | |
consumer.accept(path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment