Python chuyển sang ansi

Hướng dẫn sử dụng cho bản phát hành mới nhất có tại https. //picocli. thông tin. Dành cho người bận rộn và thiếu kiên nhẫn. cũng có Hướng dẫn nhanh

Show

1. Giới thiệu

Picocli nhằm mục đích trở thành cách dễ dàng nhất để tạo các ứng dụng dòng lệnh phong phú có thể chạy trên và ngoài JVM. Xem xét picocli?

1. 1. Tổng quan

Picocli là một khung một tệp để tạo các ứng dụng dòng lệnh Java với mã gần như bằng không. Nó hỗ trợ nhiều kiểu cú pháp dòng lệnh bao gồm POSIX, GNU, MS-DOS, v.v. Nó tạo ra các thông báo trợ giúp sử dụng có thể tùy chỉnh cao sử dụng để tương phản các yếu tố quan trọng và giảm tải nhận thức cho người dùng

Các ứng dụng dựa trên Picocli có thể hoàn thành TAB dòng lệnh hiển thị các tùy chọn, tham số tùy chọn và lệnh con có sẵn, cho bất kỳ cấp độ nào của các lệnh con lồng nhau. Các ứng dụng dựa trên Picocli có thể được biên dịch trước thành , với thời gian khởi động cực nhanh và yêu cầu bộ nhớ thấp hơn, có thể được phân phối dưới dạng một tệp thực thi duy nhất.

Picocli cho ứng dụng của bạn (trang người đàn ông HTML, PDF và Unix)

Thông báo trợ giúp sử dụng ví dụ với màu sắc và kiểu dáng ANSI

Python chuyển sang ansi

Một đặc điểm nổi bật khác của picocli là cách nó nhằm mục đích cho phép người dùng chạy các ứng dụng dựa trên picocli mà không yêu cầu picocli làm phụ thuộc bên ngoài. tất cả mã nguồn nằm trong một tệp duy nhất, để khuyến khích các tác giả ứng dụng đưa nó vào dạng nguồn

Picocli giúp bạn dễ dàng theo dõi

Làm thế nào nó hoạt động. chú thích lớp của bạn và picocli khởi tạo nó từ các đối số dòng lệnh, chuyển đổi đầu vào thành các giá trị được gõ mạnh trong các trường của lớp của bạn

Picocli cũng cung cấp một , tách biệt với API chú thích

1. 2. ứng dụng ví dụ

Ví dụ dưới đây cho thấy một ứng dụng dòng lệnh


  info.picocli
  picocli
  4.7.0
93 dựa trên picocli ngắn nhưng đầy đủ chức năng

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}

hấp dẫn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}

kịch bản Groovy

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}

Bạn có thể chạy ví dụ này trực tuyến. Hãy dùng thử mà không có đối số, với tùy chọn như


  info.picocli
  picocli
  4.7.0
94 hoặc

  info.picocli
  picocli
  4.7.0
95 hoặc với tên tệp như

  info.picocli
  picocli
  4.7.0
96 làm đối số dòng lệnh

Triển khai


  info.picocli
  picocli
  4.7.0
97 hoặc

  info.picocli
  picocli
  4.7.0
98 và lệnh của bạn có thể nằm trong một dòng mã. Phương thức

  info.picocli
  picocli
  4.7.0
99 ví dụ gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
200 để phân tích cú pháp dòng lệnh, xử lý lỗi, xử lý các yêu cầu trợ giúp về cách sử dụng và phiên bản cũng như gọi logic nghiệp vụ. Các ứng dụng có thể gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 với mã thoát được trả về để báo hiệu thành công hay thất bại cho người gọi của họ

Thuộc tính thêm các tùy chọn


  info.picocli
  picocli
  4.7.0
94 và

  info.picocli
  picocli
  4.7.0
95 vào ứng dụng của bạn

Hướng dẫn nhanh picocli hiển thị khác và giải thích chúng chi tiết hơn

Mô-đun ví dụ picocli trong kho git picocli có nhiều ví dụ khác

2. Bắt đầu

Bạn có thể thêm picocli làm phụ thuộc bên ngoài vào dự án của mình hoặc bạn có thể đưa nó làm nguồn

2. 1. Thêm dưới dạng phụ thuộc bên ngoài

Dưới đây là các ví dụ về cách định cấu hình Gradle hoặc Maven để sử dụng picocli làm phần phụ thuộc bên ngoài trong dự án của bạn

lớp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
6

maven


  info.picocli
  picocli
  4.7.0

2. 2. Thêm làm Nguồn

Để đưa vào làm nguồn, hãy lấy mã nguồn từ tệp GitHub. Sao chép và dán nó vào một tệp có tên

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
204, thêm nó vào dự án của bạn và tận hưởng

2. 3. Bộ xử lý chú thích

Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 bao gồm bộ xử lý chú thích có thể xây dựng mô hình từ các chú thích picocli tại thời điểm biên dịch thay vì thời gian chạy

Kích hoạt bộ xử lý chú thích này trong dự án của bạn là tùy chọn, nhưng rất khuyến khích. Sử dụng cái này nếu bạn quan tâm đến

  • Biên dịch kiểm tra lỗi thời gian. Bộ xử lý chú thích hiển thị lỗi đối với các chú thích và thuộc tính không hợp lệ ngay lập tức khi bạn biên dịch, thay vì trong quá trình kiểm tra khi chạy, dẫn đến chu kỳ phản hồi ngắn hơn

  • Bộ xử lý chú thích tạo và cập nhật các tệp cấu hình GraalVM theo

    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    206 trong quá trình biên dịch, để được đưa vào tệp ứng dụng. Điều này bao gồm các tệp cấu hình để phản ánh, tài nguyên và proxy động. Bằng cách nhúng các tệp cấu hình này, jar của bạn ngay lập tức được kích hoạt Graal. Trong hầu hết các trường hợp, không cần cấu hình thêm khi tạo hình ảnh gốc

2. 3. 1. tùy chọn bộ xử lý.
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
207

Bộ xử lý chú thích picocli hỗ trợ một số, trong đó quan trọng nhất là tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
207 để kiểm soát thư mục con đầu ra. các tệp được tạo được ghi vào
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
209. Một quy ước tốt là sử dụng Maven
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
210 làm giá trị;

Để định cấu hình tùy chọn này, hãy chuyển

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
211 tới trình biên dịch javac. Các ví dụ dưới đây cho thấy cách thực hiện việc này cho Maven và Gradle

2. 3. 2. Kích hoạt bộ xử lý chú thích

IDE

Trang này hiển thị các bước để định cấu hình Eclipse và IntelliJ IDEA để cho phép xử lý chú thích

Sử dụng công cụ xây dựng

lớp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
2

maven

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
7

Xem

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 README để biết thêm chi tiết

Các dự án Kotlin sử dụng Gradle

Các dự án Kotlin nên thêm plugin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
213 để kích hoạt công cụ xử lý Chú thích Kotlin (kapt), sau đó thay thế
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
214 bằng
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
215

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
2

Và thay thế

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
216 bằng
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
217

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
0

Xem

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 README để biết thêm chi tiết

2. 4. Chạy ứng dụng

Sau khi chúng tôi biên dịch thành công tệp của mình, hãy nhanh chóng xem cách chạy nó

Có nhiều cách để chạy các ứng dụng dựa trên picocli, tùy thuộc vào việc chúng tôi có đưa picocli làm nguồn hay không, có tạo jar cho ứng dụng của mình hay không và liệu chúng tôi có tạo một jar bóng mờ (còn được gọi là uber-jar) chứa tất cả các phụ thuộc hay không

Trước khi chúng tôi chạy ứng dụng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
219 của mình, hãy tạo một tệp ví dụ có tổng kiểm tra mà chúng tôi muốn in. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
1

Bây giờ, giả sử chúng ta đã tạo một jar tên là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
221 chứa
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
222 đã biên dịch của chúng ta, chúng ta có thể chạy ứng dụng với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
223. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
2

Bạn có thể muốn đóng gói ứng dụng của mình theo cách mà người dùng cuối có thể gọi nó bằng một lệnh ngắn như thế này

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
3

Xem phần để biết ý tưởng về cách thực hiện điều này

3. Tùy chọn và Thông số

Đối số dòng lệnh có thể được tách thành các tùy chọn và tham số vị trí. Tùy chọn có tên, tham số vị trí thường là các giá trị theo sau tùy chọn, nhưng chúng có thể bị trộn lẫn

Python chuyển sang ansi

Picocli có các chú thích riêng cho các tùy chọn và tham số vị trí

3. 1. Tùy chọn

Một tùy chọn phải có một hoặc nhiều

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
224. Picocli cho phép bạn sử dụng bất kỳ tên tùy chọn nào bạn muốn. Tên tùy chọn phân biệt chữ hoa chữ thường theo mặc định, nhưng đây là

Bạn có thể quan tâm đến điều này. Tuân theo các quy ước này có thể làm cho ứng dụng của bạn trực quan hơn để sử dụng cho người dùng có kinh nghiệm

Ví dụ dưới đây hiển thị các tùy chọn có một hoặc nhiều tên, tùy chọn có tham số tùy chọn và tùy chọn

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
4

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
5

Picocli khớp với tên tùy chọn để đặt giá trị trường

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
6

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
7

3. 2. Tùy chọn tương tác (Mật khẩu)

Picocli 3. 5 hỗ trợ mật khẩu được giới thiệu. đối với các tùy chọn và tham số vị trí được đánh dấu là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
225, người dùng được nhắc nhập giá trị trên bảng điều khiển. Khi chạy trên Java 6 trở lên, picocli sẽ sử dụng API để thông tin nhập của người dùng không bị lặp lại với bảng điều khiển

Từ picocli 4. 6, các ứng dụng có thể chọn lặp lại đầu vào của người dùng tới bảng điều khiển bằng cách đặt

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
227 và đặt văn bản
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
228 để kiểm soát nội dung được hiển thị trên bảng điều khiển khi yêu cầu người dùng nhập thông tin

Các tham số vị trí tương tác có giới hạn. chúng phải được theo sau bởi một tham số vị trí không tương tác. Các lệnh có tham số vị trí cuối cùng là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
225 hiện không được hỗ trợ

3. 2. 1. Thí dụ

Ví dụ dưới đây minh họa cách sử dụng tùy chọn tương tác để chỉ định mật khẩu. Từ picocli 3. 9. 6, các tùy chọn tương tác có thể sử dụng loại

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
230 thay vì Chuỗi, để cho phép các ứng dụng loại bỏ mảng sau khi sử dụng để thông tin nhạy cảm không còn nằm trong bộ nhớ

Ví dụ sử dụng

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
8

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
9

Khi lệnh này được gọi như thế này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
0

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
1

Sau đó, người dùng sẽ được nhắc nhập một giá trị

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
2

Khi chạy trên Java 6 trở lên, đầu vào của người dùng không được lặp lại với bảng điều khiển. Sau khi người dùng nhập giá trị mật khẩu và nhấn enter, phương thức

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
231 được gọi, phương thức này sẽ in nội dung như sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
3

3. 2. 2. Tương tác tùy chọn

Các tùy chọn tương tác theo mặc định khiến ứng dụng chờ đầu vào trên stdin. Đối với các lệnh cần được chạy tương tác cũng như ở chế độ hàng loạt, sẽ rất hữu ích nếu tùy chọn có thể tùy ý sử dụng một đối số từ dòng lệnh

Giá trị mặc định cho các tùy chọn tương tác là 0, nghĩa là tùy chọn này không có tham số. Từ picocli 3. 9. 6, các tùy chọn tương tác cũng có thể lấy giá trị từ dòng lệnh nếu được định cấu hình bằng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
232. (Thấy. )

Ví dụ: nếu một ứng dụng có các tùy chọn này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
4

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
5

Với đầu vào sau, trường

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
233 sẽ được khởi tạo thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
234 mà không cần nhắc người dùng nhập liệu

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
6

Tuy nhiên, nếu mật khẩu không được chỉ định, người dùng sẽ được nhắc nhập một giá trị. Trong ví dụ sau, tùy chọn mật khẩu không có tham số, vì vậy người dùng sẽ được nhắc nhập một giá trị trên bảng điều khiển

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
7

Cung cấp mật khẩu cho Batch Script một cách an toàn

Lưu ý rằng việc chỉ định mật khẩu ở dạng văn bản thuần túy trên dòng lệnh hoặc trong tập lệnh là không an toàn. Có những lựa chọn thay thế an toàn hơn

Một ý tưởng là thêm một tùy chọn khác riêng biệt (có thể được đặt tên là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
235) lấy tham số
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
236 hoặc
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
237, trong đó ứng dụng đọc mật khẩu từ tệp được chỉ định. Một ý tưởng khác là thêm một tùy chọn khác riêng biệt (có thể được đặt tên là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
238) lấy tham số tên biến môi trường, trong đó ứng dụng lấy mật khẩu từ biến môi trường của người dùng

Một lệnh kết hợp một trong hai tùy chọn này với tùy chọn tương tác

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
239 (với mặc định là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
240) cho phép người dùng cuối cung cấp mật khẩu mà không cần chỉ định mật khẩu ở dạng văn bản thuần túy trên dòng lệnh. Một lệnh như vậy có thể được thực hiện cả ở chế độ tương tác và hàng loạt

Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 có một ví dụ, được mã hóa bằng cả Java và Kotlin

Các tùy chọn tương tác và ứng dụng shell với JLine 2

Các tùy chọn tương tác không hoạt động cùng với

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
242 của JLine 2. Triển khai a sử dụng trực tiếp JLine2's
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
242 hoặc sử dụng
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
245

3. 2. 3. Buộc đầu vào tương tác

Xin lưu ý rằng picocli chỉ nhắc người dùng khi tùy chọn tương tác được chỉ định mà không có tham số

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
8

Các ứng dụng cũng cần người dùng được nhắc khi tùy chọn không được chỉ định, cần thực hiện điều này trong logic nghiệp vụ. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
9

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
0

3. 3. Tùy chọn ngắn hạn (POSIX)

Picocli hỗ trợ. một hoặc nhiều tùy chọn một ký tự không có đối số tùy chọn, theo sau tối đa một tùy chọn có đối số tùy chọn, có thể được nhóm sau một dấu phân cách '-'

Ví dụ, đưa ra lớp chú thích này

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
1

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
2

Các đối số dòng lệnh sau đều tương đương và phân tích cú pháp chúng sẽ cho kết quả giống nhau

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
3

Tùy chọn ngắn POSIX và khả năng sử dụng

Các ứng dụng có thể đưa ra một gợi ý tinh tế cho người dùng cuối rằng một tùy chọn là phổ biến và được khuyến khích bằng cách cung cấp cả tên ngắn và tên dài cho một tùy chọn. Ngược lại, việc không có quyền chọn bán khống có thể báo hiệu rằng quyền chọn này không bình thường hoặc có lẽ nên được sử dụng cẩn thận

3. 4. Tùy chọn Boolean

Tùy chọn Boolean thường không cần tham số. chỉ cần chỉ định tên tùy chọn trên dòng lệnh là đủ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
4

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
5

Giá trị của

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
246 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 theo mặc định và được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 (ngược lại với giá trị mặc định) nếu tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 được chỉ định trên dòng lệnh. Nếu tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 được chỉ định nhiều lần trên dòng lệnh, giá trị của
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
246 vẫn là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248. (Trước picocli 4. 0, giá trị của
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
246 sẽ "chuyển đổi" (lật ngược lại) cho mọi tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 trên dòng lệnh. Điều này vẫn có thể nếu được yêu cầu. )

Điều này là đủ trong hầu hết các trường hợp, nhưng picocli cung cấp các lựa chọn thay thế cho các ứng dụng cần lấy giá trị từ một thứ khác với giá trị mặc định. Khi tùy chọn được chỉ định trên dòng lệnh, trường chú thích (hoặc ) được gán một giá trị như sau

  • Nếu trình phân tích cú pháp được định cấu hình thành , giá trị ngược lại của giá trị hiện tại sẽ được gán. (Đây là mặc định trước picocli 4. 0. )

  • Nếu a được xác định, giá trị dự phòng được gán

  • Nếu tùy chọn được xác định bằng một giá trị khác không và một tham số tùy chọn đã được chỉ định trên dòng lệnh, thì tùy chọn này

  • Mặt khác, giá trị được gán là đối lập logic của giá trị mặc định

3. 5. Tùy chọn có thể phủ nhận

Từ picocli 4. 0, các tùy chọn boolean có thể là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
255

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
6

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
7

Khi một tùy chọn có thể phủ định, picocli sẽ nhận ra các bí danh phủ định của tùy chọn trên dòng lệnh

Trợ giúp sử dụng cho ví dụ trên trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
8

Đối với các tùy chọn dài kiểu *nix, các bí danh có tiền tố

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
256 cho các tên đã cho, ví dụ:
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
257. Đối với các tùy chọn kiểu Java JVM như
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
258, thì
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
259 được chuyển thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
260 và ngược lại. Theo mặc định, tên tùy chọn ngắn không được đặt bí danh phủ định. (Đây là. )

Nếu tìm thấy dạng phủ định của tùy chọn, ví dụ như

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
257, thì giá trị được đặt thành giá trị mặc định được cung cấp. Mặt khác, với một cuộc gọi thông thường, ví dụ
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262, nó được đặt ngược lại với giá trị mặc định

Các tùy chọn có thể phủ định là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 theo mặc định

Khi một tùy chọn có thể phủ định là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 theo mặc định, hãy cung cấp cho nó cả

  info.picocli
  picocli
  4.7.0
85 và

  info.picocli
  picocli
  4.7.0
86 của
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
267. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
9

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
0

Bảng bên dưới hiển thị giá trị được gán cho trường tùy chọn được chú thích cho một số chuỗi đầu vào có thể có của người dùng

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
1

3. 6. Tham số vị trí

Bất kỳ đối số dòng lệnh nào không phải là lệnh con hoặc tùy chọn (hoặc tham số tùy chọn) đều được hiểu là tham số vị trí. Các tham số vị trí thường tuân theo các tùy chọn nhưng từ picocli 2. 0, các tham số vị trí có thể được trộn lẫn với các tùy chọn trên dòng lệnh

3. 6. 1. Chỉ mục rõ ràng

Sử dụng thuộc tính (dựa trên số không)

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 để chỉ định chính xác tham số nào cần nắm bắt. Các trường mảng hoặc bộ sưu tập có thể nắm bắt nhiều giá trị

Thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 chấp nhận các giá trị phạm vi, do đó, một chú thích như
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
270 nắm bắt các đối số tại chỉ mục 2, 3 và 4. Giá trị phạm vi có thể được kết thúc mở. Ví dụ:
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
271 nắm bắt tất cả các đối số từ chỉ mục 3 trở lên

Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
2

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
3

Picocli khởi tạo các trường có giá trị tại chỉ mục đã chỉ định trong mảng đối số

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
4

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
5

Xem loại nào được hỗ trợ ngay lập tức và cách thêm loại tùy chỉnh

3. 6. 2. Bỏ qua chỉ mục

Có thể bỏ thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268. Điều này có nghĩa là những thứ khác nhau đối với tham số vị trí đơn giá trị và đa giá trị

Đối với các tham số vị trí đa giá trị (mảng hoặc tập hợp), việc bỏ qua thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 có nghĩa là trường nắm bắt tất cả các tham số vị trí (tương đương với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
274)

Đối với các tham số vị trí một giá trị, hành vi của picocli đã thay đổi kể từ phiên bản 4. 3. trước picocli 4. 3, chỉ mục mặc định cho tham số vị trí một giá trị cũng là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
274, mặc dù chỉ có thể ghi lại một giá trị (thường là đối số đầu tiên). Từ phiên bản 4. 3, picocli tự động gán một chỉ mục, dựa trên các tham số vị trí khác được xác định trong cùng một lệnh

Các chỉ mục tự động phụ thuộc vào khả năng phản chiếu Java và bộ xử lý chú thích Java để lặp qua các trường theo thứ tự khai báo trong mã nguồn. Chính thức điều này không được đảm bảo bởi thông số Java. Trong thực tế, điều này đã hoạt động trong các JVM của Oracle và OpenJDK từ Java 6, nhưng có một số rủi ro là điều này có thể không hoạt động trong tương lai hoặc trên các JVM khác. Nói chung, đối với các tham số vị trí một giá trị, sử dụng là tùy chọn an toàn hơn. (Các tham số vị trí đa giá trị có thể bỏ qua thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 một cách an toàn. )

Các phương thức không thể được lặp đi lặp lại theo thứ tự có thể dự đoán được. Đối với các ứng dụng có hoặc kết hợp các phương thức có chú thích


  info.picocli
  picocli
  4.7.0
90 và các trường có chú thích

  info.picocli
  picocli
  4.7.0
90, chúng tôi khuyên bạn nên sử dụng cho các tham số vị trí một giá trị

Xem để biết chi tiết

3. 7. Tùy chọn trộn và tham số vị trí

Từ picocli 2. 0, các tham số vị trí có thể được chỉ định ở bất kỳ đâu trên dòng lệnh, chúng không còn cần tuân theo các tùy chọn

Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
6

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
7

Bất kỳ đối số dòng lệnh nào không phải là tùy chọn hoặc lệnh con đều được hiểu là tham số vị trí

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
8

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
9

3. 8. Dấu gạch ngang kép (______084)

Khi một trong các đối số dòng lệnh chỉ là hai dấu gạch ngang mà không có bất kỳ ký tự nào được đính kèm (


  info.picocli
  picocli
  4.7.0
84), picocli diễn giải tất cả các đối số sau dưới dạng tham số vị trí, thậm chí cả các đối số khớp với tên tùy chọn

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
60

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
61

Dấu phân cách cuối tùy chọn


  info.picocli
  picocli
  4.7.0
84 làm rõ đối số nào là tham số vị trí

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
62

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
63

Dấu phân cách tùy chỉnh có thể được định cấu hình bằng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
283

Từ picocli 4. 3, một mục cho


  info.picocli
  picocli
  4.7.0
84 có thể được hiển thị trong danh sách tùy chọn của thông báo trợ giúp sử dụng của một lệnh với chú thích
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
285. Xem để biết chi tiết

3. 9. @-các tập tin

3. 9. 1. Tệp đối số cho các dòng lệnh dài

Người dùng đôi khi gặp phải các hạn chế của hệ thống về độ dài của dòng lệnh khi tạo một dòng lệnh có nhiều tùy chọn hoặc với các đối số dài cho các tùy chọn

Bắt đầu từ v2. 1. 0, picocli hỗ trợ "tệp đối số" hoặc "tệp @". Tệp đối số là tệp chứa đối số của lệnh. Khi picocli gặp một đối số bắt đầu bằng ký tự

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
286, nó sẽ mở rộng nội dung của tệp đó vào danh sách đối số

Một tệp đối số có thể bao gồm các tùy chọn và tham số vị trí trong bất kỳ kết hợp nào. Các đối số trong một tệp có thể được phân tách bằng dấu cách hoặc phân tách bằng dòng mới. Nếu một đối số chứa khoảng trắng được nhúng, hãy đặt toàn bộ đối số trong dấu ngoặc kép hoặc dấu ngoặc đơn. Trong các giá trị được trích dẫn, dấu gạch chéo ngược cần được thoát bằng dấu gạch chéo ngược khác

Ví dụ: có thể có một đường dẫn có khoảng trắng, chẳng hạn như

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
287 có thể được chỉ định là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
288 hoặc, để tránh thoát,
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
289

Các tệp đối số có giới hạn. các giá trị tham số hoặc tùy chọn được đặt trong dấu ngoặc kép không được đặt trước dấu bằng. Một cái gì đó như

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
290 không hoạt động bên trong tệp đối số. Để giải quyết vấn đề này, hãy bỏ qua dấu bằng (______ 1291) hoặc đặt toàn bộ biểu thức trong dấu ngoặc kép (________ 1292)

Các dòng bắt đầu bằng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
293 là nhận xét và bị bỏ qua. Ký tự nhận xét có thể được định cấu hình bằng
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
294 ​​và có thể tắt nhận xét bằng cách đặt ký tự nhận xét thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

Bản thân tệp có thể chứa các đối số @-file bổ sung;

Nếu tệp không tồn tại hoặc không thể đọc được thì đối số sẽ được xử lý theo nghĩa đen và không bị xóa. Nhiều tệp @ có thể được chỉ định trên dòng lệnh. Đường dẫn được chỉ định có thể là tương đối (đến thư mục hiện tại) hoặc tuyệt đối

Ví dụ: giả sử một tệp có đối số tồn tại tại

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
296, với những nội dung này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
64

Một lệnh có thể được gọi với đối số @file, như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
65

Ở trên sẽ được mở rộng thành nội dung của tệp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
66

Việc xử lý các tệp đối số được mã hóa UTF-8 khó khăn trên HĐH Windows với Java cho đến phiên bản 17. Sử dụng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
297 làm đối số VM hoặc đặt biến môi trường
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
298. Vì vậy, cả hai lệnh gọi được đưa ra bên dưới sẽ hoạt động trên dòng lệnh Windows

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
67

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
68

Trong Java 18, mã hóa mặc định đã được thay đổi từ giá trị phụ thuộc hệ thống thành luôn là UTF-8. Nếu bạn yêu cầu hành vi cũ, bạn cần đặt thuộc tính hệ thống

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
299 thành giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
700

Mở rộng tệp @ có thể được tắt bằng cách gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
701 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247. Nếu được bật, bạn vẫn có thể chuyển một tham số thực có ký tự đầu tiên là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
286 bằng cách thoát tham số đó bằng một ký hiệu bổ sung là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
286, e. g.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
705 sẽ trở thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
706 và không thể mở rộng

Tính năng này tương tự như quá trình xử lý 'Tệp đối số dòng lệnh' được hỗ trợ bởi gcc, javadoc và javac. Tài liệu cho các công cụ này có nhiều chi tiết hơn. Xem ví dụ tài liệu cho

Nếu bạn cho rằng người dùng của mình có thể thấy tính năng này (@files) hữu ích, thì bạn có thể cân nhắc thêm một tùy chọn vào ứng dụng của mình để tạo @file cho các đối số đã chỉ định. Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 trên GitHub có một ví dụ để giúp bạn bắt đầu

3. 9. 2. @-files Trợ giúp sử dụng

Từ picocli 4. 2, một mục cho

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 có thể được hiển thị trong danh sách tùy chọn và tham số của thông báo trợ giúp sử dụng của một lệnh với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
709. Xem để biết chi tiết

3. 9. 3. Định dạng đơn giản hóa

Từ picocli 3. 8. 1, định dạng tệp đối số đơn giản hơn cũng được hỗ trợ trong đó mọi dòng (ngoại trừ dòng trống và dòng nhận xét) được hiểu là một đối số. Các đối số chứa khoảng trắng không cần phải được trích dẫn, nhưng không thể có các đối số có dòng mới được nhúng hoặc có các đối số chuỗi trống không có dấu ngoặc kép. Từ picocli 3. 9, định dạng đối số đơn giản hơn này hoàn toàn tương thích với định dạng tệp đối số của _ _ _ _ _

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
710

Bạn có thể yêu cầu picocli sử dụng định dạng tệp đối số được đơn giản hóa theo chương trình với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
711 hoặc bằng cách đặt thuộc tính hệ thống
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
712 mà không có giá trị hoặc có giá trị
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
267 (không phân biệt chữ hoa chữ thường). Thuộc tính hệ thống rất hữu ích để cho phép người dùng cuối kiểm soát định dạng

4. Gõ mạnh mọi thứ

Khi các tùy chọn dòng lệnh và tham số vị trí được ánh xạ tới các trường được chú thích, giá trị văn bản được chuyển đổi thành loại của trường được chú thích

4. 1. Các loại tích hợp

Ngay lập tức, picocli có thể chuyển đổi các chuỗi đối số dòng lệnh thành một số loại dữ liệu phổ biến

Hầu hết các loại dựng sẵn đều hoạt động với Java 5, nhưng picocli cũng có một số trình chuyển đổi mặc định cho các loại Java 7 như

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
237 và các loại Java 8 như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
715, v.v. Các bộ chuyển đổi này được tải bằng phản chiếu và chỉ khả dụng khi chạy trên phiên bản Java hỗ trợ chúng. Xem danh sách dưới đây để biết chi tiết

  • bất kỳ kiểu nguyên thủy Java hoặc trình bao bọc của chúng

  • bất kỳ

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    716

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    717,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    718,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    719

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    720,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    721

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    722

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    723

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    724

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    725

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    726 (đối với các giá trị ở định dạng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    727)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    728,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    729

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    730

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    731 (từ picocli 2. 2, cho tên lớp đủ điều kiện)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    732 (từ picocli 2. 2, đối với Chuỗi
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    733 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    734)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    735 (từ picocli 2. 2, đối với mã ISO 4217 của tiền tệ)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    736 (từ picocli 2. 2, cho InetAddress hoặc tên của giao diện mạng)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    737 (từ picocli 2. 2, để biết ID cho Múi giờ)

Bộ chuyển đổi được tải bằng cách sử dụng sự phản chiếu

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    738 (từ picocli 2. 2, yêu cầu Java 7 trở lên)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    739 đối tượng giá trị.
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    715,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    741,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    742,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    743,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    744,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    745,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    746,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    747,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    748,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    749,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    750,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    751,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    752,
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    753 (từ picocli 2. 2, yêu cầu Java 8 trở lên, gọi phương thức
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    754 của các lớp này)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    755 (đối với các giá trị ở bất kỳ định dạng nào trong số các định dạng ________ 2756, ________ 2757, ________ 2758 hoặc ________ 2759)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    760 (từ picocli 2. 2, đối với các giá trị ở định dạng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    761 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    762)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    763 (từ picocli 2. 2, đối với url cơ sở dữ liệu có dạng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    764)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    765 (từ picocli 2. 2, đối với URL cơ sở dữ liệu có dạng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    764)

Đôi khi tải bộ chuyển đổi với phản xạ là không mong muốn. Sử dụng thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
767 để chỉ định danh sách các tên lớp đủ điều kiện được phân tách bằng dấu phẩy mà bộ chuyển đổi không được tải. Biểu thức chính quy được hỗ trợ. Ví dụ: gọi chương trình với
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
768 sẽ không tải bộ chuyển đổi loại cho
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
755 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
760

4. 2. Bộ chuyển đổi loại tùy chỉnh

Đăng ký trình chuyển đổi loại tùy chỉnh để xử lý các loại dữ liệu khác với các loại tích hợp sẵn ở trên

4. 2. 1. Bộ chuyển đổi loại thông số đơn

Bộ chuyển đổi tùy chỉnh cần triển khai giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
771

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
69

Ví dụ


  info.picocli
  picocli
  4.7.0
0

Bộ chuyển đổi loại tùy chỉnh có thể được chỉ định cho một tùy chọn hoặc tham số vị trí cụ thể với thuộc tính chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
772. Điều này được mô tả chi tiết hơn trong phần, nhưng đây là một ví dụ nhanh

Java


  info.picocli
  picocli
  4.7.0
1

Kotlin


  info.picocli
  picocli
  4.7.0
2

hấp dẫn


  info.picocli
  picocli
  4.7.0
3

Tập lệnh Groovy


  info.picocli
  picocli
  4.7.0
4

Các chương trình Groovy có thể sử dụng

Ngoài ra, bộ chuyển đổi loại tùy chỉnh có thể được đăng ký cho mỗi loại trong mỗi lệnh bằng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
774. Tất cả các tùy chọn và tham số vị trí với loại được chỉ định sẽ được chuyển đổi bởi bộ chuyển đổi được chỉ định

Sau khi đăng ký bộ chuyển đổi tùy chỉnh, hãy gọi phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
775 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
776 trên phiên bản
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 nơi bộ chuyển đổi được đăng ký. (Không thể sử dụng phương thức tĩnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
778. ) Ví dụ

Java


  info.picocli
  picocli
  4.7.0
5

Kotlin


  info.picocli
  picocli
  4.7.0
6

Java 8 lambdas giúp dễ dàng đăng ký bộ chuyển đổi tùy chỉnh

Java


  info.picocli
  picocli
  4.7.0
7

Kotlin


  info.picocli
  picocli
  4.7.0
8

Lưu ý về tiểu ban. bộ chuyển đổi được chỉ định sẽ được đăng ký với đối tượng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 và tất cả các tiểu ban (và các tiểu ban lồng nhau) đã được thêm vào trước khi bộ chuyển đổi được đăng ký. Các tiểu ban được thêm sau này sẽ không có bộ chuyển đổi được thêm tự động. Để đảm bảo bộ chuyển đổi loại tùy chỉnh có sẵn cho tất cả các tiểu ban, hãy đăng ký bộ chuyển đổi kiểu cuối cùng, sau khi thêm các tiểu ban

4. 2. 2. Bộ chuyển đổi loại đa thông số

Một số loại có nhiều hơn một tham số. Giao diện

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243 có thể được sử dụng để triển khai bộ chuyển đổi loại đa tham số

Java


  info.picocli
  picocli
  4.7.0
9

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
20

Xem các phần trên để biết thêm chi tiết

Đảm bảo rằng mọi lớp lồng nhau là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
781, nếu không picocli sẽ không thể khởi tạo chúng

4. 3. Xử lý đầu vào không hợp lệ

Nếu người dùng chỉ định đầu vào không hợp lệ, bộ chuyển đổi loại tùy chỉnh sẽ đưa ra một ngoại lệ. Bất kỳ ngoại lệ nào cũng được và sẽ dẫn đến một thông báo như bên dưới được hiển thị cho người dùng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
21

Thông báo lỗi trên là chung chung và hợp lý đối với nhiều trường hợp ngoại lệ, nhưng đôi khi bạn muốn kiểm soát nhiều hơn đối với thông báo lỗi hiển thị cho người dùng. Để đạt được điều này, thay vào đó hãy ném một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
782. Khi ném
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
783, picocli sẽ hiển thị thông báo lỗi cho biết tùy chọn có vấn đề, theo sau là văn bản thông báo ngoại lệ. Đầu ra kết quả trông giống như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
22

Dưới đây là ví dụ về trình chuyển đổi tùy chỉnh đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
783

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
23

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
24

Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 trên GitHub có một ví dụ hoạt động tối thiểu mà bạn có thể chạy trong trình chỉnh sửa trực tuyến của chúng tôi

Lưu ý rằng khi một tùy chọn có , trình phân tích cú pháp picocli không thể cho biết mỗi đối số tiếp theo thuộc về tùy chọn đó hay thuộc về tham số vị trí. Nó sẽ cố gắng gán cho tùy chọn trước, nhưng sẽ có ngoại lệ chuyển đổi loại có nghĩa là nó đã đạt đến cuối các tham số cho tùy chọn đó và thay vào đó, đối số này phải được gán cho một tham số vị trí

Nếu sau đó nó không thể tìm thấy tham số vị trí, thay vào đó, nó sẽ được hiển thị cho người dùng cuối

4. 4. Bộ chuyển đổi loại dành riêng cho tùy chọn

Picocli 2. 2 đã thêm thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
772 vào chú thích

  info.picocli
  picocli
  4.7.0
89 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
788. Điều này cho phép một tùy chọn hoặc tham số vị trí cụ thể sử dụng một bộ chuyển đổi khác với bộ chuyển đổi sẽ được sử dụng theo mặc định dựa trên loại trường

Ví dụ: đối với một trường cụ thể, bạn có thể muốn sử dụng trình chuyển đổi ánh xạ tên hằng được xác định trong

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
789 thành giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790 của các hằng này, nhưng bất kỳ trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790 nào khác sẽ không bị ảnh hưởng bởi điều này và nên tiếp tục sử dụng trình chuyển đổi int tiêu chuẩn

Ví dụ sử dụng

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
25

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
26

Thực hiện ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
27

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
28

Điều này cũng có thể hữu ích cho các ứng dụng cần bộ chuyển đổi loại tùy chỉnh nhưng muốn sử dụng các phương pháp tiện lợi tĩnh (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
778,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
795). Chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
772 không yêu cầu phiên bản
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 để có thể sử dụng nó với các phương thức tiện lợi tĩnh

Các bộ chuyển đổi kiểu được khai báo với thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
772 cần phải có một hàm tạo không đối số công khai để được khởi tạo, trừ khi a được cài đặt để khởi tạo các lớp

Nếu trình chuyển đổi kiểu của bạn được khai báo là lớp lồng nhau, hãy đảm bảo bạn đánh dấu lớp này là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
781, nếu không picocli sẽ không thể khởi tạo lớp trình chuyển đổi lồng nhau của bạn

4. 5. Mảng, Bộ sưu tập, Bản đồ

Bắt đầu từ picocli 2. 0, thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200 không còn cần thiết cho các trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202. picocli sẽ suy ra loại phần tử bộ sưu tập từ loại chung. (Thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200 vẫn hoạt động như cũ, nó chỉ là tùy chọn trong hầu hết các trường hợp. )

4. 5. 1. Mảng và Bộ sưu tập

Nhiều tham số có thể được ghi lại cùng nhau trong một mảng hoặc trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201. Các phần tử mảng hoặc bộ sưu tập có thể là bất kỳ loại nào mà a được đăng ký. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
29

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
70

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
71

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
72

Nếu một bộ sưu tập được trả về từ bộ chuyển đổi kiểu, thì nội dung của bộ sưu tập sẽ được thêm vào tham số trường hoặc phương thức, chứ không phải chính bộ sưu tập đó

Nếu tham số trường hoặc phương thức là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295, picocli sẽ khởi tạo nó khi tùy chọn hoặc tham số vị trí được khớp thành công. Nếu loại
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201 không phải là một lớp cụ thể, picocli sẽ cố gắng hết sức để khởi tạo nó dựa trên loại trường.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
207,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
208,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
209,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
210, nếu không thì,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
211

Các tùy chọn đa giá trị và tham số vị trí có thể được xác định bằng biểu thức chính quy

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 để cho phép người dùng cuối chỉ định nhiều giá trị trong một tham số. Xem phần để biết chi tiết

4. 5. 2. bản đồ

Picocli 1. 0 đã giới thiệu hỗ trợ cho các trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202 tương tự như thuộc tính hệ thống của Java
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
214 hoặc thuộc tính Gradle
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
215

Các trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202 có thể có bất kỳ loại nào cho khóa và giá trị của chúng miễn là a được đăng ký cho cả loại khóa và giá trị. Các loại khóa và giá trị được suy ra từ các tham số loại chung của bản đồ. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
73

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
74

Tùy chọn bản đồ có thể được chỉ định nhiều lần với các cặp khóa-giá trị khác nhau. (Thấy. )

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
75

Nếu trường được chú thích là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295, picocli sẽ khởi tạo nó khi tùy chọn hoặc tham số vị trí được khớp. Nếu loại
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202 không phải là một lớp cụ thể, picocli sẽ khởi tạo một
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
219 để duy trì thứ tự đầu vào

Trên dòng lệnh, khóa và giá trị phải được phân tách bằng ký tự

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220

Các tùy chọn bản đồ và tham số vị trí có thể được xác định bằng biểu thức chính quy

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 để cho phép người dùng cuối chỉ định nhiều giá trị trong một tham số. Xem phần để biết chi tiết

4. 5. 3. Tham số bản đồ chỉ có khóa

Theo mặc định, picocli dự kiến ​​các tùy chọn Bản đồ và tham số vị trí trông giống như

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
222, tức là tham số tùy chọn hoặc tham số vị trí dự kiến ​​sẽ có phần khóa và phần giá trị, được phân tách bằng ký tự
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220. Nếu đây không phải là trường hợp, picocli hiển thị thông báo lỗi cho người dùng.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
224

Từ picocli 4. 6, các ứng dụng có thể chỉ định một

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
225 để cho phép người dùng cuối chỉ chỉ định phần chính.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
225 đã chỉ định được đưa vào bản đồ khi người dùng cuối chỉ chỉ định một khóa. Loại giá trị có thể là. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
76

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
77

Điều này cho phép đầu vào như sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
78

Đầu vào ở trên sẽ cho kết quả sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
79

Lưu ý rằng mô tả tùy chọn có thể chứa biến sẽ được thay thế bằng giá trị dự phòng bản đồ thực tế khi trợ giúp sử dụng được hiển thị

4. 5. 4. Thuộc tính hệ thống

Một yêu cầu chung đối với các ứng dụng dòng lệnh là hỗ trợ cú pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
214 để cho phép người dùng cuối thiết lập các thuộc tính hệ thống

Ví dụ bên dưới sử dụng loại

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202 để xác định một

  info.picocli
  picocli
  4.7.0
89- ủy quyền tất cả các cặp khóa-giá trị cho
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
232. Lưu ý việc sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
233 để cho phép

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
20

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
21

4.6. Optional

Từ phiên bản 4. 6, picocli hỗ trợ các loại giá trị đơn được bao bọc trong đối tượng vùng chứa

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
227 khi chạy trên Java 8 trở lên. Nếu tùy chọn hoặc tham số vị trí không được chỉ định trên dòng lệnh, picocli sẽ gán giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
235 thay vì
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
22

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
23

Picocli chỉ hỗ trợ hạn chế cho các loại

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
227. chỉ các loại giá trị đơn và các giá trị trong một
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
202 (chứ không phải các khóa. ) có thể được bọc trong hộp chứa
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
239. Không thể kết hợp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
227 với mảng hoặc các lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201 khác

4. 7. Các loại trường trừu tượng

Loại trường có thể là một giao diện hoặc một lớp trừu tượng. Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200 có thể được sử dụng để kiểm soát đối với từng trường loại cụ thể mà giá trị chuỗi sẽ được chuyển đổi thành. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
24

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
25

4. 7. 1. Bản đồ và Bộ sưu tập với các yếu tố trừu tượng

Đối với bản đồ thô và bộ sưu tập hoặc khi sử dụng thuốc generic có ký tự đại diện không giới hạn như

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
243 hoặc khi tham số loại bản thân chúng là các lớp trừu tượng như
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
244 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
245, sẽ không có đủ thông tin để chuyển đổi sang loại mạnh hơn. Theo mặc định, các giá trị Chuỗi thô được thêm vào các bộ sưu tập đó

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200 có thể được chỉ định để chuyển đổi sang loại mạnh hơn Chuỗi. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
26

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
27

4. 8. Kiểu liệt kê

Bạn nên sử dụng các loại

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
716 cho các tùy chọn hoặc tham số vị trí với một bộ giá trị hợp lệ giới hạn. Picocli không chỉ xác thực đầu vào mà còn cho phép bạn xem thông báo trợ giúp sử dụng với
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
248. Nó cũng cho phép hoàn thành dòng lệnh để đề xuất các ứng cử viên hoàn thành cho các giá trị của tùy chọn này

Theo mặc định, khớp giá trị enum phân biệt chữ hoa chữ thường, nhưng kể từ picocli 3. 4 điều này có thể được kiểm soát bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
249 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
250

5. Giá trị mặc định

Có thể xác định giá trị mặc định cho một tùy chọn hoặc tham số vị trí, được chỉ định khi người dùng không chỉ định tùy chọn này hoặc tham số vị trí trên dòng lệnh

Định cấu hình giá trị mặc định đảm bảo rằng trường có chú thích


  info.picocli
  picocli
  4.7.0
89 hoặc

  info.picocli
  picocli
  4.7.0
90 sẽ được thiết lập, phương thức được chú thích sẽ được gọi và khi sử dụng API có lập trình, phương thức đó sẽ được gọi, ngay cả khi tùy chọn hoặc tham số vị trí không được chỉ định trên

5. 1. info.picocli picocli 4.7.0 85 Chú thích

Cách được đề xuất để cung cấp cho một tùy chọn hoặc tham số vị trí một giá trị mặc định là sử dụng thuộc tính chú thích


  info.picocli
  picocli
  4.7.0
85. Điều này hoạt động chính xác với các nhóm đối số, các phương thức được chú thích bởi

  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90, đồng thời cho phép bộ xử lý chú thích phát hiện và sử dụng các giá trị mặc định

Đối với và , không có cách nào khác ngoài việc sử dụng thuộc tính chú thích


  info.picocli
  picocli
  4.7.0
85. Ví dụ: đối với giao diện chú thích

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
28

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
29

Ví dụ về việc sử dụng thuộc tính


  info.picocli
  picocli
  4.7.0
85 trong tùy chọn của phương thức lệnh

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
00

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
01

Lưu ý rằng bạn có thể sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260 trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261 của tùy chọn hoặc tham số vị trí và picocli sẽ là giá trị mặc định thực tế

5. 2. Giá trị trường

Đối với các trường chú thích, có thể khai báo trường có giá trị

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
02

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
03

Xác định giá trị mặc định bằng cách gán giá trị tại phần khai báo trường có những hạn chế

  • khi tùy chọn được sử dụng trong một nhóm đối số, trợ giúp sử dụng

  • bộ xử lý chú thích của picocli chỉ có thể phát hiện các giá trị mặc định trong chú thích, không phải trong khai báo trường. Ứng dụng của bạn có thể không hoạt động chính xác với các tính năng trong tương lai như tài liệu được tạo từ chú thích

5. 3. Các biến trong giá trị mặc định

Bản thân giá trị mặc định cũng có thể chứa. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
04

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
05

Picocli sẽ tra cứu giá trị của biến

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
262 trong thuộc tính hệ thống, biến môi trường và gói tài nguyên và cuối cùng sử dụng giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
263 nếu không tìm thấy giá trị nào cho bất kỳ tra cứu nào trong số này

5. 4. Nhà cung cấp mặc định

Cuối cùng, bạn có thể chỉ định nhà cung cấp mặc định trong chú thích


  info.picocli
  picocli
  4.7.0
91

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
06

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
07

Nhà cung cấp mặc định cho phép bạn nhận các giá trị mặc định từ tệp cấu hình hoặc một số vị trí trung tâm khác. Các nhà cung cấp mặc định cần triển khai giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
265

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
08

Xem ví dụ về nhà cung cấp mặc định để biết cách triển khai ví dụ

Nếu lệnh có nhà cung cấp mặc định được định cấu hình và tham số tùy chọn hoặc vị trí có giá trị mặc định được định cấu hình, thì trước tiên, picocli sẽ cố gắng tìm giá trị trong nhà cung cấp mặc định. Nếu nhà cung cấp mặc định không có giá trị cho tùy chọn hoặc tham số vị trí đó, thì giá trị mặc định được định cấu hình trên tùy chọn hoặc tham số vị trí sẽ được sử dụng

5. 5. Thuộc tính DefaultProvider

Từ picocli 4. 1, các ứng dụng có thể sử dụng triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
266 tích hợp để tải các giá trị mặc định từ tệp thuộc tính

Theo mặc định, việc triển khai này sẽ cố gắng tìm một tệp thuộc tính có tên là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
267 trong thư mục chính của người dùng, trong đó
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
268 là tên của lệnh. Nếu một lệnh có bí danh ngoài tên của nó, những bí danh này cũng được sử dụng để cố gắng tìm tệp thuộc tính. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
09

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
10

Ở trên sẽ cố tải các giá trị mặc định từ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
269. Vị trí của tệp thuộc tính cũng có thể được kiểm soát bằng thuộc tính hệ thống
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
270 (trong ví dụ này là ______3271), trong trường hợp đó, giá trị của thuộc tính phải là đường dẫn đến tệp chứa các giá trị mặc định

Vị trí của tệp thuộc tính cũng có thể được chỉ định theo chương trình. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
11

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
12

5. 5. 1. Thuộc tính Định dạng DefaultProvider

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
266 mong đợi tệp thuộc tính ở định dạng java
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
273 tiêu chuẩn

Đối với các tùy chọn, khóa là hoặc của tùy chọn, không có tiền tố. Vì vậy, đối với tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262, khóa sẽ là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
275 và đối với tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
276, khóa sẽ là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
277

Đối với tham số vị trí, khóa là tham số hoặc tham số vị trí

Người dùng cuối có thể không biết

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 của các tùy chọn và tham số vị trí của bạn là gì, vì vậy hãy đảm bảo ghi lại điều đó cùng với ứng dụng của bạn

5. 5. 2. Lệnh con Giá trị mặc định

Các giá trị mặc định cho các tùy chọn và tham số vị trí của các lệnh con có thể được bao gồm trong tệp thuộc tính cho lệnh cấp cao nhất, để người dùng cuối chỉ cần duy trì một tệp duy nhất. Điều này có thể đạt được bằng cách đặt trước các phím cho các tùy chọn và tham số vị trí bằng tên đủ điều kiện của lệnh của chúng. Ví dụ: để cung cấp cho tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
280 của lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
279 một giá trị mặc định là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
281, hãy xác định một khóa là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
282 và gán cho nó một giá trị mặc định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
13

5. 6. info.picocli picocli 4.7.0 86 Chú thích

Nếu một tùy chọn được xác định bằng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
232, thì nó. Nếu một tùy chọn như vậy được chỉ định mà không có giá trị trên dòng lệnh, nó sẽ được gán giá trị dự phòng

Thuộc tính chú thích


  info.picocli
  picocli
  4.7.0
86 đã được giới thiệu trong picocli 4. 0; . 3) một Chuỗi rỗng đã được chỉ định

Điều này khác với


  info.picocli
  picocli
  4.7.0
85, được chỉ định nếu tùy chọn hoàn toàn không được chỉ định trên dòng lệnh

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
14

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
15

Cho kết quả sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
16

Mọi giá trị Chuỗi được chuyển đổi thành loại tùy chọn trước khi được gán cho tùy chọn. Các tùy chọn và tham số vị trí có thể xác định a nếu cần

Lưu ý rằng mô tả tùy chọn có thể chứa

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
287 sẽ được thay thế bằng giá trị dự phòng thực tế khi trợ giúp sử dụng được hiển thị

cũng có thể xác định một


  info.picocli
  picocli
  4.7.0
86 để chỉ định giá trị sẽ được đặt khi tùy chọn được khớp trên dòng lệnh, bất kể giá trị mặc định là gì. Điều này có thể hữu ích khi người dùng cuối có thể định cấu hình mặc định, chẳng hạn

5. 7. Là một giá trị mặc định?

Đôi khi một ứng dụng muốn biết liệu một giá trị tùy chọn đã được chỉ định trên dòng lệnh hay liệu giá trị mặc định đã được gán hay chưa

Bạn có thể sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
289 để phát hiện xem một tùy chọn có thực sự khớp trên dòng lệnh hay không và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
290 để nhận giá trị (đã chuyển đổi kiểu) được chỉ định trên dòng lệnh.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
291 sẽ trả về giá trị được gán (có thể là đối số dòng lệnh hoặc có thể là giá trị mặc định)

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
17

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
18

5. 8. Null giá trị mặc định

Để gán

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295 làm giá trị mặc định, các ứng dụng có thể sử dụng giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
293 trong chú thích cho

  info.picocli
  picocli
  4.7.0
85 và

  info.picocli
  picocli
  4.7.0
86

Nếu loại của tùy chọn hoặc tham số vị trí là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
296, thì picocli sẽ gán giá trị mặc định là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
235 thay vì
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
19

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
20

6. Nhiều giá trị

Các tùy chọn đa giá trị và tham số vị trí là các trường được chú thích có thể nắm bắt nhiều giá trị từ dòng lệnh

6. 1. Nhiều lần xuất hiện

6. 1. 1. Tùy chọn lặp đi lặp lại

Cách đơn giản nhất để tạo một tùy chọn đa giá trị là khai báo một trường được chú thích có kiểu là một mảng, tập hợp hoặc bản đồ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
21

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
22

Người dùng có thể chỉ định cùng một tùy chọn nhiều lần. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
23

Mỗi giá trị được thêm vào mảng hoặc bộ sưu tập

6. 1. 2. Nhiều thông số vị trí

Tương tự cho các tham số vị trí đa giá trị

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
24

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
25

Người dùng có thể chỉ định nhiều tham số vị trí. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
26

Một lần nữa, mỗi giá trị được thêm vào mảng hoặc bộ sưu tập

6. 1. 3. Tùy chọn Boolean lặp đi lặp lại

Các tùy chọn Boolean với nhiều giá trị được hỗ trợ kể từ picocli 2. 1. 0

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
27

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
28

Người dùng có thể chỉ định nhiều tùy chọn cờ boolean mà không cần tham số. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
29

Ví dụ trên dẫn đến sáu giá trị

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 được thêm vào mảng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
000

6. 2. Tách Regex

Các tùy chọn và tham số cũng có thể chỉ định một biểu thức chính quy

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 được sử dụng để chia từng tham số tùy chọn thành các chuỗi con nhỏ hơn. Mỗi chuỗi con này được chuyển đổi thành loại bộ sưu tập hoặc mảng. Thấy

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
30

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
31

Một đối số dòng lệnh đơn lẻ như sau sẽ được tách ra và ba giá trị

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790 được thêm vào mảng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
32

Tương tự cho

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
33

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
34

Với tùy chọn trên, các đối số dòng lệnh như sau được hiểu là một tập hợp các cặp khóa-giá trị thay vì một chuỗi

Ghi chú. người dùng cuối cần trích dẫn tham số tùy chọn để ngăn không cho các ký tự thanh dọc

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
003 được trình bao hiểu là chỉ thị "đường ống" để kết nối các quy trình

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
35

Kết quả đầu vào ở trên trong trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
004 được gán một
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
219 với các cặp khóa-giá trị sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
36

Xem để biết chi tiết về cách xử lý các trường hợp phức tạp hơn

Picocli 4. 3 đã giới thiệu thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
006 để kiểm soát nội dung được hiển thị trong phần tóm tắt của thông báo trợ giúp sử dụng. Xem để biết chi tiết

6. 3. Arity

Đôi khi bạn muốn xác định một tùy chọn yêu cầu nhiều hơn một tham số tùy chọn cho mỗi lần xuất hiện tùy chọn trên dòng lệnh

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007 cho phép bạn kiểm soát chính xác số lượng tham số sẽ sử dụng cho mỗi lần xuất hiện tùy chọn

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007 có thể chỉ định số lượng chính xác các tham số bắt buộc hoặc một phạm vi có số lượng tham số tối thiểu và tối đa. Giá trị tối đa có thể là giới hạn trên chính xác hoặc có thể là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
009 để biểu thị bất kỳ số lượng tham số nào. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
37

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
38

Một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
010 được ném khi ít hơn số lượng tham số tối thiểu được chỉ định trên dòng lệnh

Sau khi sử dụng hết số lượng tham số tối thiểu, picocli sẽ kiểm tra từng đối số dòng lệnh tiếp theo để xem đó là tham số bổ sung hay tùy chọn mới. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
39

Tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
011 có đối số
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
012 nhưng thay vì sử dụng tất cả các tham số, đối số
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
013 được công nhận là một tùy chọn riêng biệt

6. 4. Biến hạn chế Arity

6. 4. 1. Tùy chọn Arity biến và Tùy chọn không xác định

Như đã đề cập trong phần , trong khi xử lý các tham số cho một tùy chọn có biến đối xứng, khi một tùy chọn đã biết, lệnh con hoặc gặp phải, picocli sẽ ngừng thêm tham số vào tùy chọn đối biến.

Tuy nhiên, theo mặc định, trình phân tích cú pháp picocli không xử lý đặc biệt đối với (các giá trị "trông giống như" một tùy chọn) khi xử lý các tham số cho một tùy chọn có tính chất thay đổi. Các giá trị như vậy chỉ đơn giản là được sử dụng bởi tùy chọn với arity biến. Từ picocli 4. 4 đây là

6. 4. 2. Tùy chọn Arity biến và Tham số vị trí

Hãy cẩn thận khi xác định các lệnh có cả tùy chọn với biến arity (như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
014) và tham số vị trí

Trình phân tích cú pháp picocli "tham lam" khi nó xử lý các tham số tùy chọn cho các tùy chọn có tính chất thay đổi. nó xem xét giá trị theo sau tên tùy chọn và nếu giá trị đó có thể được lấy làm tham số (không phải tùy chọn hoặc lệnh con khác và chưa đạt đến mức tối đa) thì nó sẽ xử lý giá trị dưới dạng tham số cho tùy chọn đó. Đây có thể không phải lúc nào cũng là những gì bạn muốn

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
40

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
41

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
015 được chỉ định trên dòng lệnh, điều này dẫn đến lỗi.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
016

Người dùng có thể sử dụng và phân biệt đầu vào với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
017, nhưng điều này có thể không rõ ràng đối với nhiều người dùng. Một ý tưởng là phân cách trong trợ giúp sử dụng

Một giải pháp thay thế tốt hơn có thể là thiết kế lại lệnh của bạn để tránh sự mơ hồ hoàn toàn. Một ý tưởng là sử dụng (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
018 trong ví dụ của chúng tôi) và sử dụng thuộc tính để cho phép người dùng chỉ định nhiều giá trị trong một đối số như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
019. Nếu
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
020 chỉ nhận một tham số duy nhất, thì đầu vào của người dùng như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
021 không còn mơ hồ nữa

6. 5. Arity mặc định

Nếu không chỉ định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007, số lượng tham số phụ thuộc vào loại trường

6. 5. 1. Tùy chọn Arity

Bảng 1. Mặc định
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007 cho các trường

  info.picocli
  picocli
  4.7.0
89@Loại trường tùy chọnDefault ArityNotes

boolean

0. 1

Tùy chọn Boolean theo mặc định không yêu cầu tham số tùy chọn. Trường được đặt ngược lại với giá trị mặc định khi tên tùy chọn được nhận dạng. (Điều này có thể là. )

Loại đơn giá trị (e. g. ,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
717,
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
236)

1

Tên tùy chọn phải được theo sau bởi một giá trị

Loại đa giá trị (mảng, tập hợp hoặc bản đồ)

1

Tên tùy chọn phải được theo sau bởi một giá trị

Trước picocli 2. 0, các tùy chọn đa giá trị được sử dụng để tham lam sử dụng càng nhiều đối số càng tốt cho đến khi gặp một tùy chọn hoặc lệnh con khác. Nếu ứng dụng của bạn dựa trên hành vi trước đó, bạn cần chỉ định rõ ràng một loại tùy chọn là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
028 khi di chuyển sang picocli 2. 0

6. 5. 2. Arity tham số vị trí

ban 2. Mặc định
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007 cho các trường

  info.picocli
  picocli
  4.7.0
90@Parameters Field TypeDefault ArityNotes

boolean

1

Tham số vị trí của loại

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
031 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
032 yêu cầu một giá trị. Chỉ
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 hoặc
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 (không phân biệt chữ hoa chữ thường) là các giá trị hợp lệ

Loại đơn giá trị (e. g. ,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
717,
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
236)

1

Một tham số cần thiết cho mỗi vị trí

Loại đa giá trị (mảng, tập hợp hoặc bản đồ)

0. 1

Đối với các tham số vị trí đa giá trị (mảng, tập hợp hoặc bản đồ), các giá trị là tùy chọn, không

Các trường


  info.picocli
  picocli
  4.7.0
90 được áp dụng cho đối số dòng lệnh nếu chỉ mục của chúng khớp với vị trí của đối số. Chỉ số mặc định là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
039, nghĩa là tất cả các vị trí. Trường

  info.picocli
  picocli
  4.7.0
90 với
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
041 được áp dụng nhiều lần. một lần cho mỗi tham số vị trí trên dòng lệnh

Khi một trường


  info.picocli
  picocli
  4.7.0
90 được áp dụng (vì chỉ mục của nó khớp với chỉ mục của tham số vị trí), trường này có thể sử dụng 0, một hoặc nhiều đối số, tùy thuộc vào tính chất của nó

6. 6. Giá trị tùy chọn

6. 6. 1. Tham số tùy chọn tùy chọn

Khi một tùy chọn được xác định bằng

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
232, nó có thể có hoặc không có giá trị tham số

Xác định giá trị nào được chỉ định khi tùy chọn được chỉ định mà không có giá trị, trong khi xác định giá trị nào được chỉ định khi tùy chọn hoàn toàn không được chỉ định

6. 6. 2. Các trường hợp sử dụng tham số tùy chọn

Tính năng này thường được sử dụng khi một ứng dụng muốn kết hợp hai tùy chọn thành một. sự hiện diện hoặc vắng mặt của tùy chọn có thể được sử dụng như cờ boolean để kích hoạt một số hành vi và giá trị tùy chọn có thể được sử dụng để sửa đổi hành vi này

Một trường hợp sử dụng ví dụ là một tùy chọn bật ghi nhật ký khi xuất hiện, với một giá trị tùy chọn để đặt mức nhật ký. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
42

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
43

Một trường hợp sử dụng ví dụ khác là

6. 6. 3. Giới hạn tham số tùy chọn

Hãy cẩn thận khi xác định các lệnh có cả tùy chọn với tham số tùy chọn và tham số vị trí

Trình phân tích cú pháp picocli "tham lam" khi xử lý các tham số tùy chọn. nó xem xét giá trị theo sau tên tùy chọn và nếu giá trị đó có thể là một tham số (không phải tùy chọn hoặc lệnh con khác) thì nó sẽ xử lý giá trị dưới dạng tham số cho tùy chọn đó. Đây có thể không phải lúc nào cũng là những gì bạn muốn

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
44

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
45

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
044 được chỉ định trên dòng lệnh, điều này dẫn đến lỗi.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
016

Người dùng có thể sử dụng và phân biệt đầu vào bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
046, nhưng điều này có thể không rõ ràng đối với nhiều người dùng. Một ý tưởng là phân cách trong trợ giúp sử dụng. Một ý tưởng khác là tận dụng giới thiệu với picocli 4. 6

Một cách khác là tránh sử dụng các tham số tùy chọn và sử dụng arity mặc định trong trường hợp này để loại bỏ hoàn toàn sự mơ hồ

7. Đối số bắt buộc

7. 1. Tùy chọn bắt buộc

Các tùy chọn có thể được đánh dấu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
047 để người dùng bắt buộc phải chỉ định chúng trên dòng lệnh. Khi một tùy chọn bắt buộc không được chỉ định, một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
010 được ném ra từ phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
754. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
46

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
47

Các đối số dòng lệnh sau đây sẽ dẫn đến một ngoại lệ phàn nàn rằng thiếu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
050

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
48

Các đối số dòng lệnh sau đây sẽ được chấp nhận

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
49

7. 2. Thông số bắt buộc

Giá trị đơn


  info.picocli
  picocli
  4.7.0
90 luôn là bắt buộc, vì tham số vị trí giá trị đơn theo mặc định

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
007 có thể được sử dụng để bắt buộc phải có nhiều giá trị

  info.picocli
  picocli
  4.7.0
90

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
50

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
51

Các đối số dòng lệnh sau đây sẽ dẫn đến một ngoại lệ phàn nàn rằng thiếu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
055

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
52

Các đối số dòng lệnh sau đây sẽ được chấp nhận

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
53

7. 3. Tùy chọn với Thông số tùy chọn

Thấy

8. Nhóm đối số

Picocli 4. 0 giới thiệu một chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 mới và tương đương với chương trình
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
057 của nó

Nhóm đối số có thể được sử dụng để xác định

  • tùy chọn loại trừ lẫn nhau

  • các tùy chọn phải cùng xảy ra (tùy chọn phụ thuộc)

  • phần tùy chọn trong thông báo trợ giúp sử dụng

  • lặp lại các đối số tổng hợp

Để tạo một nhóm bằng API chú thích, hãy chú thích một trường hoặc phương thức bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056. Loại của trường đề cập đến lớp chứa các tùy chọn và tham số vị trí trong nhóm. (Đối với các phương thức giao diện được chú thích, đây sẽ là kiểu trả về, đối với các phương thức setter được chú thích trong một lớp cụ thể, đây sẽ là kiểu tham số của setter. )

Picocli sẽ khởi tạo lớp này khi cần để nắm bắt các giá trị đối số dòng lệnh trong các trường và phương thức được chú thích


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 của lớp này

hiện không thể được sử dụng trong Nhóm đối số. Các ứng dụng muốn sử dụng lại Nhóm đối số trên các tiểu ban cần sử dụng. Xem ví dụ này để chia sẻ Nhóm đối số xác định các tùy chọn chung giữa các tiểu ban

8. 1. Tùy chọn loại trừ lẫn nhau

Chú thích một trường hoặc phương thức với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
061 để tạo một nhóm các tùy chọn và tham số vị trí loại trừ lẫn nhau. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
54

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
55

Ví dụ trên định nghĩa một lệnh với các tùy chọn loại trừ lẫn nhau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
062,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
063 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
064

Bản thân nhóm có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
065 xác định số lần nhóm có thể được chỉ định trong lệnh. Giá trị mặc định là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066, nghĩa là theo mặc định, một nhóm có thể được bỏ qua hoặc chỉ định một lần. Trong ví dụ này, nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
067, vì vậy nhóm phải xảy ra một lần. một trong các tùy chọn độc quyền phải xảy ra trên dòng lệnh

Tóm tắt của lệnh này là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
56

Khi một trong các tùy chọn trong nhóm phù hợp, picocli tạo một thể hiện của lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
068 và gán nó cho trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
070 được chú thích bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056

Lưu ý rằng các tùy chọn được định nghĩa là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
071;

Kể từ picocli 4. 1. 2, tất cả các tùy chọn trong một nhóm độc quyền sẽ tự động được coi là bắt buộc, ngay cả khi chúng không được đánh dấu là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
071 trong chú thích. Các ứng dụng sử dụng các phiên bản cũ hơn của picocli nên đánh dấu tất cả các tùy chọn trong các nhóm độc quyền theo yêu cầu

Picocli sẽ xác thực các đối số và đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
073 nếu nhiều đối số loại trừ lẫn nhau được chỉ định. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
57

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
58

Đối với nhóm trên, chỉ có thể chỉ định một trong các tùy chọn. Bất kỳ sự kết hợp nào khác của các tùy chọn hoặc không có tùy chọn nào đều không hợp lệ

Picocli sẽ không khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh. Đối với các nhóm tùy chọn (các nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066 - mặc định), điều này có nghĩa là trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 có thể vẫn là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

8. 2. Tùy chọn phụ thuộc lẫn nhau

8. 2. 1. Tổng quan

Chú thích một trường hoặc phương thức với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
078 để tạo một nhóm các tùy chọn phụ thuộc và các tham số vị trí phải cùng xuất hiện. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
59

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
60

Ví dụ trên xác định một lệnh có các tùy chọn phụ thuộc

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
062,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
063 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
064 phải đồng thời xảy ra

Bản thân nhóm có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
065 xác định số lần nhóm có thể được chỉ định trong lệnh. Trong ví dụ này, nhóm sử dụng bội số mặc định,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066, nghĩa là nhóm có thể được bỏ qua hoặc chỉ định một lần

Tóm tắt của lệnh này là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
61

Khi tùy chọn đầu tiên trong nhóm được khớp, picocli tạo một thể hiện của lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
084 và gán nó cho trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
086 chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056

Lưu ý rằng các tùy chọn được định nghĩa là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
071;

Picocli sẽ xác thực các đối số và đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
010 nếu không phải tất cả các đối số phụ thuộc đã được chỉ định. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
62

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
63

Picocli sẽ không khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh. Đối với các nhóm tùy chọn (các nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066 - mặc định), điều này có nghĩa là trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 có thể vẫn là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

8. 2. 2. Tùy chọn không bắt buộc trong các nhóm phụ thuộc lẫn nhau

Trong các nhóm phụ thuộc lẫn nhau có thể có một hoặc nhiều lựa chọn không bắt buộc. Điều này khác với , trong đó tất cả các tùy chọn luôn được yêu cầu

Thật hữu ích khi có thể xác định một nhóm đồng thời là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
093 để cả
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
094 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
095 đều hợp lệ trên dòng lệnh, nhưng không phải là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
096 chẳng hạn. Điều này có thể được thực hiện bằng cách đánh dấu tùy chọn tùy chọn bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
097, như trong ví dụ bên dưới

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
64

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
65

Nhiều tùy chọn có thể là tùy chọn trong các nhóm phụ thuộc lẫn nhau, nhưng nên có ít nhất một tùy chọn bắt buộc trong nhóm (hoặc không có nhiều điểm khi sử dụng nhóm phụ thuộc lẫn nhau)

8. 3. Phần tùy chọn trong Trợ giúp sử dụng

8. 3. 1. Sử dụng tiêu đề để kích hoạt các phần tùy chọn

Ví dụ bên dưới sử dụng các nhóm để xác định các phần tùy chọn trong phần trợ giúp sử dụng. Khi một nhóm có một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
098 khác null (hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
099), các tùy chọn trong nhóm được cung cấp tiêu đề được chỉ định trong thông báo trợ giúp sử dụng. Thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
099 có thể được sử dụng để lấy văn bản tiêu đề từ gói tài nguyên của lệnh

Điều này hoạt động cho các nhóm loại trừ lẫn nhau hoặc cùng xảy ra, nhưng cũng có thể xác định một nhóm không xác thực mà chỉ tạo một phần tùy chọn trong trợ giúp sử dụng

Chú thích một trường hoặc phương thức với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
101 để tạo một nhóm chỉ nhằm mục đích hiển thị. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
66

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
67

Điều này in thông báo trợ giúp sử dụng sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
68

Lưu ý rằng văn bản tiêu đề phải kết thúc bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
102 để chèn một dòng mới giữa văn bản tiêu đề và tùy chọn đầu tiên. Điều này là để thống nhất với các tiêu đề khác trong phần trợ giúp sử dụng, chẳng hạn như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
103

Picocli sẽ không khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh. Đối với các nhóm tùy chọn (các nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066 - mặc định), điều này có nghĩa là trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 có thể vẫn là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

8. 3. 2. Thứ tự phần tùy chọn

Các tùy chọn không có trong bất kỳ nhóm đối số nào luôn được hiển thị trước bất kỳ phần tùy chọn nhóm nào

Thứ tự của các phần tùy chọn nhóm có thể được kiểm soát bằng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
108. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
69

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
70

8. 3. 3. Đánh đổi xác thực

Lưu ý rằng cài đặt

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
109 có nghĩa là picocli sẽ không xác thực đầu vào của người dùng cho nhóm. Ví dụ: ngay cả đối với các nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
110, khi người dùng cuối chỉ định nhóm nhiều lần, không có lỗi nào được hiển thị. Nếu nhóm là trường một giá trị, thì chỉ lần xuất hiện cuối cùng được lưu trữ và các lần xuất hiện trước đó sẽ bị loại bỏ một cách âm thầm

Nếu cần xác thực, đề xuất là biến trường chứa nhóm thành một bộ sưu tập và thực hiện. Ví dụ: để đảm bảo rằng bộ sưu tập này chỉ chứa một phần tử duy nhất

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
71

8. 4. Lặp lại các nhóm đối số tổng hợp

Ví dụ dưới đây cho thấy cách nhóm có thể bao gồm các nhóm khác và cách sử dụng mảng và tập hợp để nắm bắt các nhóm lặp lại (với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
065 lớn hơn một)

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
72

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
73

Trong ví dụ trên, trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
112 được chú thích xác định một nhóm hỗn hợp phải được chỉ định ít nhất một lần và có thể được chỉ định nhiều lần (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
113), trên dòng lệnh. Lưu ý rằng đối với các nhóm đa giá trị, loại trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 phải là một tập hợp hoặc một mảng để nắm bắt nhiều phiên bản
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
115 chứa các giá trị được so khớp trên dòng lệnh

Tóm tắt của lệnh này là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
74

Mỗi khi nhóm được khớp, picocli tạo một thể hiện của lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
115 và thêm nó vào danh sách
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
112

Bản thân lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
115 chứa hai nhóm. một nhóm các tùy chọn phụ thuộc không bắt buộc (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066) phải cùng xảy ra và một nhóm các tùy chọn loại trừ lẫn nhau khác, là bắt buộc (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
067)

Ví dụ dưới đây minh họa

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
75

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
76

Picocli sẽ không khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh. Đối với các nhóm tùy chọn (các nhóm có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
066 - mặc định), điều này có nghĩa là trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 có thể vẫn là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295. Nếu ứng dụng được chỉ định một Bộ sưu tập không phải ____1295 trong khai báo trường (e. g. ,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
126), thì bộ sưu tập sẽ trống nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh

8. 5. Giá trị mặc định trong nhóm đối số

Các tùy chọn trong một nhóm đối số được áp dụng khi ít nhất một tùy chọn trong nhóm được khớp trên dòng lệnh và picocli khởi tạo đối tượng người dùng của nhóm

Picocli sẽ không khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 (và vì vậy không có giá trị mặc định nào được áp dụng) nếu không có tùy chọn nhóm nào được chỉ định trên dòng lệnh

8. 5. 1. Hiển thị Giá trị Mặc định trong Trợ giúp Sử dụng Nhóm

Các tùy chọn được sử dụng trong các nhóm đối số phải xác định các giá trị mặc định thông qua chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
128

Khi các giá trị mặc định được xác định trong chú thích, biến

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260 có thể được sử dụng để mô tả các tùy chọn trong một nhóm đối số. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
77

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
78

Khi giá trị mặc định được xác định trong chú thích, trợ giúp sử dụng sẽ hiển thị giá trị mặc định chính xác

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
79

Picocli sẽ không thể truy xuất các giá trị mặc định được xác định bằng cách gán một giá trị trong phần khai báo của trường có chú thích


  info.picocli
  picocli
  4.7.0
89 trong một nhóm. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
80

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
81

Khi giá trị mặc định được xác định trong khai báo trường chứ không phải trong chú thích, trợ giúp sử dụng cho các tùy chọn trong nhóm hiển thị không chính xác

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295 là giá trị mặc định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
82

8. 5. 2. Gán giá trị mặc định trong nhóm đối số

Các ứng dụng cần thực hiện thêm công việc cho các tùy chọn nhóm đối số với các giá trị mặc định. Picocli không khởi tạo nhóm nếu không có tùy chọn nào trong nhóm được chỉ định trên dòng lệnh, vì vậy các ứng dụng cần thực hiện việc này theo cách thủ công

Dưới đây là một số đề xuất để sử dụng các giá trị mặc định trong tùy chọn nhóm đối số và tham số vị trí

  • chỉ định các giá trị mặc định trong cả chú thích

    
      info.picocli
      picocli
      4.7.0
    
    89 và trong giá trị ban đầu của trường có chú thích
    
      info.picocli
      picocli
      4.7.0
    
    89. Vâng, điều đó có nghĩa là một số trùng lặp. Khuyến nghị này cũng áp dụng cho vị trí
    
      info.picocli
      picocli
      4.7.0
    
    90

  • ứng dụng cần khởi tạo thủ công trường có chú thích

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    056. Thông tin chi tiết theo dõi bên dưới

Giá trị mặc định trong chú thích


  info.picocli
  picocli
  4.7.0
89 hoặc

  info.picocli
  picocli
  4.7.0
90 có nghĩa là picocli có thể trong trợ giúp sử dụng và giá trị ban đầu có nghĩa là bất kỳ phiên bản mới nào của nhóm chứa tùy chọn sẽ có giá trị mặc định được gán cho trường tùy chọn đó

Ví dụ bên dưới hiển thị một tùy chọn xác định giá trị mặc định trong chú thích cũng như trong giá trị ban đầu của trường

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
83

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
84

Tiếp theo, ứng dụng cần khởi tạo trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
056 theo cách thủ công. Có một sự đánh đổi

  • khởi tạo trường có chú thích

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    056 trong khai báo rất đơn giản và ngắn gọn nhưng các ứng dụng không thể dễ dàng phát hiện liệu một tùy chọn nhóm có được chỉ định trên dòng lệnh hay không

  • để lại trường có chú thích

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    056
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    295 trong phần khai báo cho phép các ứng dụng dễ dàng phát hiện xem một tùy chọn nhóm có được chỉ định trên dòng lệnh hay không, nhưng mã nhiều hơn một chút

Ví dụ dưới đây cho thấy ý tưởng đầu tiên. khởi tạo đối tượng nhóm trong khai báo. Bằng cách này, đối tượng nhóm không bao giờ là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295 và (nếu bạn đã làm theo đề xuất trước đó) tất cả các trường tùy chọn trong đối tượng nhóm này sẽ có giá trị mặc định làm giá trị ban đầu của chúng

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
85

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
86

Ngoài ra, các ứng dụng có thể khởi tạo các đối tượng nhóm trong logic nghiệp vụ. trong phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794. Điều này cho phép ứng dụng xác định xem người dùng có chỉ định giá trị cho bất kỳ tùy chọn nào trong nhóm hay không

Ví dụ dưới đây minh họa việc khởi tạo các đối tượng nhóm trong logic nghiệp vụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
87

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
88

8. 6. Tham số vị trí

Khi một tham số vị trí


  info.picocli
  picocli
  4.7.0
90 là một phần của một nhóm, thì
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 của nó là chỉ mục trong nhóm, không nằm trong lệnh

Dưới đây là một ví dụ về ứng dụng sử dụng nhóm tham số vị trí lặp lại

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
89

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
90

Chạy chương trình trên với đầu vào này

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
91

Tạo đầu ra sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
92

8. 7. Nhóm đối số Hạn chế

  • Không thể xác định các tùy chọn có cùng tên trong nhiều nhóm. Tương tự, không thể xác định tùy chọn bên ngoài nhóm có cùng tên với tùy chọn khác thuộc nhóm

  • Tham số vị trí trong một nhóm hoạt động tốt, nhưng hãy cẩn thận (hoặc tránh) xác định tham số vị trí trong nhiều nhóm hoặc tham số vị trí trong một nhóm cũng như bên ngoài nhóm. Các tham số vị trí được so khớp theo chỉ mục và trong khi chỉ mục của một nhóm được đặt lại khi gặp bội số nhóm mới, thì chỉ mục của các tham số vị trí bên ngoài một nhóm chỉ tăng lên và không bao giờ được đặt lại

  • Một số mối quan hệ giữa các tùy chọn không thể được thể hiện với các nhóm đối số picocli. Nói chung, các nhóm đối số picocli chỉ có thể biểu thị mối quan hệ mà bạn có thể viết một bản tóm tắt dòng lệnh với mọi tùy chọn chỉ xảy ra một lần. Ví dụ: không thể sử dụng các nhóm đối số để tạo mối quan hệ với các tùy chọn loại trừ

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    147, trong đó
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    062 yêu cầu một tùy chọn khác
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    064.
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    150, đồng thời
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    063 độc lập với
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    064.
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    153. Ứng dụng có thể cần thực hiện một số xác thực có lập trình trong những trường hợp như vậy

9. Thực thi lệnh

Phân tích đối số dòng lệnh là bước đầu tiên. Một ứng dụng trong thế giới thực mạnh mẽ cần xử lý một số tình huống

  1. Dữ liệu nhập của người dùng không hợp lệ

  2. Trợ giúp sử dụng do người dùng yêu cầu (có thể dành cho một tiểu ban)

  3. Trợ giúp về phiên bản do người dùng yêu cầu (có thể dành cho một tiểu ban)

  4. Không có điều nào ở trên. chúng ta có thể chạy logic nghiệp vụ (có khả năng cho một tiểu ban)

  5. Logic nghiệp vụ có thể đưa ra một ngoại lệ

Picocli 4. 0 giới thiệu phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 để xử lý tất cả các tình huống trên trong một dòng mã. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
93

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
94

Với phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154, mã ứng dụng có thể cực kỳ nhỏ gọn

Java

________ 295 ________ 296

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
95
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
98

Mặc dù chỉ dài 15 dòng nhưng đây là một ứng dụng chính thức, với các tùy chọn ngoài tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249. Phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 sẽ hiển thị thông tin phiên bản hoặc trợ giúp sử dụng nếu người dùng yêu cầu và thông tin người dùng nhập không hợp lệ sẽ dẫn đến thông tin hữu ích. Nếu đầu vào của người dùng hợp lệ, logic nghiệp vụ sẽ được gọi. Cuối cùng, phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 trả về một phương thức có thể được sử dụng để gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 nếu muốn

Một lệnh có thể thực thi được nếu đối tượng người dùng của nó triển khai


  info.picocli
  picocli
  4.7.0
97 hoặc

  info.picocli
  picocli
  4.7.0
98 hoặc là một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
165 được chú thích bởi

  info.picocli
  picocli
  4.7.0
91. Ví dụ theo bên dưới

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 thay thế các phương thức cũ hơn là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
795 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
170

Phần này hiển thị một ví dụ về mã soạn sẵn có thể được bỏ qua bằng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154

9. 1. Mã thoát

Nhiều ứng dụng dòng lệnh trả về mã thoát để biểu thị thành công hay thất bại. Số không thường có nghĩa là thành công, mã thoát khác không thường được sử dụng cho các lỗi, nhưng ngoài ra, ý nghĩa khác nhau đối với mỗi ứng dụng

Phương pháp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
200 được giới thiệu trong picocli 4. 0 trả về một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790 và các ứng dụng có thể sử dụng giá trị trả về này để gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 nếu muốn. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
99

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
00

Các phiên bản cũ hơn của picocli có một số hỗ trợ mã thoát giới hạn trong đó picocli sẽ gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201, nhưng điều này hiện không được dùng nữa

9. 2. Tạo mã thoát

Các lớp có chú thích


  info.picocli
  picocli
  4.7.0
91 triển khai

  info.picocli
  picocli
  4.7.0
98 và

  info.picocli
  picocli
  4.7.0
91- có thể chỉ cần trả về một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
790 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
180 và giá trị này sẽ được trả về từ
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
200. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
01

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
02

Các lệnh có đối tượng người dùng triển khai


  info.picocli
  picocli
  4.7.0
97 có thể triển khai giao diện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
183 để tạo mã thoát. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
03

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
04

9. 3. Mã thoát ngoại lệ

Theo mặc định, phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 trả về
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
185 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
186) khi thành công,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
187 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
188) khi xảy ra ngoại lệ trong phương thức Runnable, Có thể gọi hoặc lệnh và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
189 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
190) cho đầu vào không hợp lệ. (Đây là những giá trị phổ biến theo). Điều này có thể được tùy chỉnh với chú thích

  info.picocli
  picocli
  4.7.0
91. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
05

Ngoài ra, các ứng dụng có thể định cấu hình

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
192 để ánh xạ một ngoại lệ cụ thể tới mã thoát

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
06

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
07

Khi người dùng cuối chỉ định đầu vào không hợp lệ, phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 sẽ in thông báo lỗi theo sau là thông báo trợ giúp sử dụng của lệnh và trả về mã thoát. Điều này có thể bằng cách định cấu hình một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194

Nếu logic nghiệp vụ của lệnh đưa ra một ngoại lệ, thì phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 sẽ in dấu vết ngăn xếp của ngoại lệ đó và trả về một mã thoát. Điều này có thể được tùy chỉnh bằng cách định cấu hình một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196

9. 4. Phần mã thoát trợ giúp sử dụng

Theo mặc định, thông báo trợ giúp sử dụng không bao gồm thông tin về mã thoát. Các ứng dụng gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 cần định cấu hình thông báo trợ giúp sử dụng để hiển thị chi tiết mã thoát, với thuộc tính chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
198 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
199 hoặc lập trình bằng cách gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
200 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
201

Xem để biết chi tiết

9. 5. Cấu hình thực thi

Các phương thức sau đây có thể được sử dụng để định cấu hình hành vi của phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154

  • nhận/thiết lập

  • nhận/đặtErr

  • get/setColorScheme - xem

  • get/setExecutionStrategy - xem

  • lấy/đặt tham số ExceptionHandler - xem

  • nhận/đặt Thực thi ExceptionHandler - xem

  • nhận/setExitCodeExceptionMapper

Các phương pháp trên không áp dụng được với (và bị bỏ qua bởi) các điểm vào khác như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
754,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
778,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
795,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
209 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
170

9. 6. di cư

Các phiên bản cũ hơn của picocli được hỗ trợ các phương pháp thuận tiện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
795 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
170 tương tự như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 nhưng hỗ trợ hạn chế cho cấu hình trình phân tích cú pháp và hỗ trợ hạn chế cho mã thoát. Các phương pháp này không được dùng nữa từ picocli 4. 0. Các phần bên dưới hiển thị một số cách sử dụng phổ biến và cách đạt được điều tương tự với API
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154

9. 6. 1. Tùy chỉnh luồng đầu ra và cài đặt ANSI

Trước

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
08

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
09

Sau đó

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
10

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
11

9. 6. 2. Giá trị trả về từ Callable hoặc Method

Trước

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
12

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
13

Sau đó

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
14

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
15

9. 6. 3. Gọi phương thức lệnh

Trước

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
16

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
17

Sau đó

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
18

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
19

9. 6. 4. Thực hiện các lệnh với các lệnh con

Giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
217 không được dùng nữa trong picocli 4. 0 ủng hộ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
218. Trình xử lý tích hợp sẵn hiện có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
219,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
220 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
221 triển khai giao diện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
218 và vẫn có thể được sử dụng

  • trình xử lý

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    219 in trợ giúp nếu được yêu cầu và nếu không thì sẽ nhận lệnh hoặc lệnh con được chỉ định cuối cùng và cố gắng thực thi nó dưới dạng
    
      info.picocli
      picocli
      4.7.0
    
    97,
    
      info.picocli
      picocli
      4.7.0
    
    98 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    165. Đây là chiến lược thực thi mặc định

  • trình xử lý

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    221 in trợ giúp nếu được yêu cầu và nếu không thì thực thi lệnh cấp cao nhất dưới dạng
    
      info.picocli
      picocli
      4.7.0
    
    97,
    
      info.picocli
      picocli
      4.7.0
    
    98 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    165

  • trình xử lý

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    220 in trợ giúp nếu được yêu cầu và nếu không thì thực thi tất cả các lệnh và lệnh con mà người dùng đã chỉ định trên dòng lệnh dưới dạng các tác vụ
    
      info.picocli
      picocli
      4.7.0
    
    97,
    
      info.picocli
      picocli
      4.7.0
    
    98 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    165

Trước

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
20

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
21

Sau đó

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
22

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
23

Có thể sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
235 để lấy giá trị trả về từ lệnh con Có thể gọi hoặc Phương thức

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
24

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
25

9. 7. Thực thi lệnh DIY

Ngoài ra, các ứng dụng có thể muốn sử dụng trực tiếp phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 và viết logic thực thi lệnh "Tự làm" của riêng chúng

Ví dụ dưới đây bao gồm các tình huống phổ biến sau

  1. Xử lý đầu vào của người dùng không hợp lệ và báo cáo bất kỳ sự cố nào cho người dùng (có khả năng đề xuất các tùy chọn và lệnh con thay thế cho các lỗi chính tả đơn giản nếu chúng tôi muốn trở nên lạ mắt)

  2. Kiểm tra xem người dùng có yêu cầu trợ giúp sử dụng hay không và in trợ giúp này và hủy xử lý nếu trường hợp này xảy ra

  3. Kiểm tra xem người dùng có yêu cầu thông tin phiên bản hay không và in thông tin này và hủy xử lý nếu trường hợp này xảy ra

  4. Nếu không có điều nào ở trên, hãy chạy logic nghiệp vụ của ứng dụng

  5. Xử lý bất kỳ lỗi nào xảy ra trong logic nghiệp vụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
26

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
27

Phương pháp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
200 tương đương với phương pháp trên và bổ sung xử lý chính xác các lệnh con

9. 8. Xử lý lỗi

Bên trong, phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 phân tích cú pháp đầu vào của người dùng được chỉ định và điền vào các tùy chọn và tham số vị trí được xác định bởi các chú thích. Khi người dùng chỉ định đầu vào không hợp lệ, điều này được xử lý bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194

Sau khi phân tích cú pháp đầu vào của người dùng, logic nghiệp vụ của lệnh được gọi. phương pháp chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794 hoặc

  info.picocli
  picocli
  4.7.0
91. Khi một ngoại lệ được đưa ra bởi logic nghiệp vụ, điều này sẽ được xử lý bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196

Trong hầu hết các trường hợp, trình xử lý mặc định là đủ, nhưng các phần bên dưới cho biết cách chúng có thể được tùy chỉnh

9. 8. 1. Người dùng nhập không hợp lệ

Khi người dùng chỉ định đầu vào không hợp lệ, trình phân tích cú pháp sẽ đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244. Trong phương pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154, các ngoại lệ như vậy được bắt và chuyển đến
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194

Trình xử lý ngoại lệ tham số mặc định in thông báo lỗi mô tả sự cố, theo sau là tùy chọn nhập sai hoặc thông báo trợ giúp sử dụng đầy đủ của lệnh có vấn đề. Cuối cùng, trình xử lý trả về một. Điều này là đủ cho hầu hết các ứng dụng

Đôi khi bạn muốn hiển thị một thông báo ngắn hơn. Ví dụ: tiện ích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
247 không hiển thị trợ giúp sử dụng đầy đủ khi gặp đối số không hợp lệ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
28

Bạn có thể tùy chỉnh cách ứng dụng của mình xử lý dữ liệu nhập không hợp lệ của người dùng bằng cách đặt tùy chỉnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
29

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
30

Trường hợp triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194 có thể giống như thế này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
31

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
32

9. 8. 2. Ngoại lệ logic nghiệp vụ

Khi logic nghiệp vụ đưa ra một ngoại lệ, ngoại lệ này sẽ bị bắt và chuyển đến

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196

Việc xử lý ngoại lệ thực thi mặc định dẫn đến dấu vết ngăn xếp của ngoại lệ được in và trả về. Điều này là đủ cho hầu hết các ứng dụng

Nếu bạn đã thiết kế logic nghiệp vụ của mình để đưa ra các ngoại lệ với thông báo lỗi cho người dùng, bạn muốn in thông báo lỗi này thay vì theo dõi ngăn xếp. Điều này có thể được thực hiện bằng cách cài đặt một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196 tùy chỉnh, như thế này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
33

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
34

Trường hợp triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196 có thể trông giống như thế này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
35

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
36

9. 9. Trường hợp sử dụng hiếm

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
253 là cách được đề xuất để thực thi ứng dụng dòng lệnh của bạn, vì nó cung cấp khả năng xử lý ngoại lệ có thể định cấu hình, xử lý các yêu cầu của người dùng về trợ giúp sử dụng hoặc thông tin phiên bản, dẫn đến mã ứng dụng ngắn và đơn giản và không bao giờ đưa ra ngoại lệ

Tuy nhiên, có thể có các trường hợp sử dụng mà phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 không phù hợp. Cách khác là sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
255 và xử lý đối tượng kết quả là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
235 trong ứng dụng của bạn. Phần này cho thấy những gì liên quan đến việc làm như vậy

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 có thể hữu ích khi viết mã kiểm tra trình phân tích cú pháp hoặc khi phương thức

  info.picocli
  picocli
  4.7.0
99 của ứng dụng của bạn được ứng dụng khác gọi. Các phần sau đây đi vào một số chi tiết

9. 9. 1. Ví dụ mã kiểm tra trình phân tích cú pháp

Phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 hữu ích trong mã kiểm tra chỉ thực hiện logic phân tích cú pháp mà không liên quan đến logic nghiệp vụ. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
37

hấp dẫn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
38

9. 9. 2. Thấm ngoại lệ lên

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 không bao giờ đưa ra ngoại lệ và đối với một số ứng dụng, điều này là không mong muốn

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 cũng có thể hữu ích khi phương thức

  info.picocli
  picocli
  4.7.0
99 của ứng dụng của bạn được gọi bởi ứng dụng khác và ứng dụng khác này chịu trách nhiệm xử lý lỗi

Một trường hợp sử dụng phổ biến là khi ứng dụng của bạn được gọi là một phần của bản dựng. Ví dụ: Maven cung cấp mục tiêu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
263 với
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
264 và Gradle cung cấp tương tự các tác vụ Exec và JavaExec

Mục tiêu Maven

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
264 gọi lớp mục tiêu trong cùng một quy trình Maven. Trong trường hợp này, chúng tôi không muốn gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201, vì nó sẽ dừng toàn bộ quá trình Maven và ngoài ra, chúng tôi muốn Maven xử lý các ngoại lệ do ứng dụng dòng lệnh đưa ra

Một ý tưởng là cung cấp một phương pháp


  info.picocli
  picocli
  4.7.0
99 riêng sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 thay vì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
39

Sau đó, trong cấu hình bản dựng, hãy gọi lớp lồng nhau

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
270 thay vì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
271 để công cụ bản dựng xử lý bất kỳ ngoại lệ nào và tránh gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201

9. 9. 3. Hệ thống. thoát hay không?

Một giải pháp thay thế cho giải pháp trên là quyết định trong thời gian chạy có nên gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 hay không

Việc triển khai ví dụ bên dưới minh họa cách sử dụng các thuộc tính hệ thống để xác định xem có nên gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 hay không

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
40

Công cụ riêng của Picocli sử dụng phương pháp này. khi công cụ này được gọi với (______2275), nó sẽ gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 khi xảy ra lỗi

10. Thẩm định

10. 1. Xác thực tích hợp

Picocli cung cấp một số hình thức xác thực hạn chế

  • tùy chọn với một hoặc nhiều

  • tùy chọn một giá trị được chỉ định nhiều lần

  • tùy chọn

  • tùy chọn

  • của các tùy chọn độc quyền hoặc phụ thuộc

10. 2. Xác thực tùy chỉnh

Hầu hết các ứng dụng sẽ cần thực hiện xác thực bổ sung để xác minh một số quy tắc kinh doanh

Các ứng dụng sử dụng có thể thấy hữu ích khi ném

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244 khi xác thực không thành công. bất kỳ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
279 nào sẽ bị bắt và xử lý bởi tính năng tích hợp của picocli, hiển thị thông báo lỗi in đậm màu đỏ và theo sau là thông báo trợ giúp sử dụng

Để xây dựng một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244, bạn cần phiên bản
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 nơi xảy ra lỗi. Điều này có thể được lấy từ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282, do đó có thể được lấy từ một trường. Các phần dưới đây cho thấy một số ví dụ

10. 2. 1. Xác thực giá trị đơn

Các phương thức được chú thích bằng


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 có thể thực hiện xác thực đầu vào đơn giản bằng cách ném một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244 khi các giá trị không hợp lệ được chỉ định trên dòng lệnh

Ví dụ sau xác thực rằng giá trị được chỉ định cho tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
287 là một số nguyên tố

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
41

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
42

10. 2. 2. Xác thực kết hợp tùy chọn

Một tình huống phổ biến khác là sự kết hợp của nhiều tùy chọn và tham số vị trí là hợp lệ. Một cách để thực hiện điều này là thực hiện xác thực như vậy khi bắt đầu logic nghiệp vụ

Ví dụ sau xác thực rằng ít nhất một trong các tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
288,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
289 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
290 được chỉ định

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
43

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
44

10. 2. 3. JSR-380 BeanValidation

Nếu bạn muốn giữ xác thực của mình dựa trên khai báo và chú thích, hãy xem JSR 380

JSR-380 là một đặc điểm kỹ thuật của API Java để xác thực bean, một phần của JavaEE và JavaSE, đảm bảo rằng các thuộc tính của bean đáp ứng các tiêu chí cụ thể, sử dụng các chú thích như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
291,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
292 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
293

Picocli wiki có một ví dụ đầy đủ, dưới đây là một đoạn trích

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
45

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
46

10. 2. 4. Sử dụng Chiến lược thực thi tùy chỉnh để xác thực

BeanValidation JSR-380 ở trên cũng có thể được thực hiện bằng một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
218 tùy chỉnh thực hiện xác thực trước khi thực hiện lệnh. Điều này di chuyển logic xác thực vào một lớp riêng biệt. Picocli gọi logic này, loại bỏ nhu cầu gọi phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
295 khỏi logic nghiệp vụ

Một chiến lược thực thi tùy chỉnh như vậy có thể trông như thế này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
47

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
48

Ứng dụng có thể kết nối chiến lược thực thi tùy chỉnh này như sau

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
49

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
50

11. Cấu hình trình phân tích cú pháp

11. 1. phân biệt chữ hoa chữ thường

Theo mặc định, tất cả các tùy chọn và lệnh con đều phân biệt chữ hoa chữ thường. Từ picocli 4. 3, trường hợp nhạy cảm có thể cấu hình. Phân biệt chữ hoa chữ thường có thể được tắt trên toàn cầu, cũng như trên cơ sở từng lệnh

Để chuyển đổi độ phân biệt chữ hoa chữ thường cho tất cả các lệnh, hãy sử dụng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
296 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
297. Sử dụng các phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
298 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
299 để cung cấp cho một số lệnh có độ nhạy chữ hoa chữ thường khác với các lệnh khác

Nếu có thể, picocli sẽ cố gắng ngăn chặn sự mơ hồ. khi nhiều tùy chọn có cùng tên được đăng ký trong một lệnh, một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
300 sẽ được ném ra. Khi nhiều tiểu ban có cùng tên được đăng ký trong một lệnh, một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
301 sẽ được ném ra

Khi tắt phân biệt chữ hoa chữ thường, nguyên tắc tương tự cũng được áp dụng. nhiều tùy chọn có tên chỉ khác nhau trong trường hợp không thể đăng ký trong lệnh. Tương tự, không thể đăng ký nhiều tiểu ban khi tên của chúng chỉ khác nhau trong trường hợp

Khi sự kết hợp của các tùy chọn POSIX giống với một tùy chọn dài, picocli sẽ ưu tiên tùy chọn dài. Đây là trường hợp bất kể độ nhạy chữ hoa chữ thường, nhưng hãy lưu ý rằng khi tắt độ nhạy chữ hoa chữ thường, khả năng xảy ra các va chạm như vậy sẽ tăng lên. Ví dụ: nếu một lệnh có các tùy chọn POSIX

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
062,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
063 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
064 và một tùy chọn dài
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
305, thì khi người dùng chỉ định
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
306, picocli sẽ nhận ra đó là tùy chọn dài
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
305, không phải là tùy chọn POSIX

Xem gói phân biệt chữ hoa chữ thường trong

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 để biết một số ví dụ

11. 2. Các tùy chọn và tiểu ban viết tắt

kể từ picocli 4. 4, trình phân tích cú pháp có thể nhận ra các tùy chọn viết tắt và các lệnh con. Điều này cần được kích hoạt rõ ràng với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
309 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
310

11. 2. 1. Chữ viết tắt được công nhận

Khi bật tính năng viết tắt, người dùng có thể chỉ định (các) chữ cái đầu tiên của thành phần đầu tiên và tùy chọn của một hoặc nhiều thành phần tiếp theo của một tùy chọn hoặc tên lệnh con

"Các thành phần" được phân tách bằng ký tự gạch ngang

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
311 hoặc theo trường hợp, vì vậy, ví dụ: cả
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
312 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
313 đều có hai thành phần

Khi chỉ phân biệt chữ hoa chữ thường, ký tự gạch ngang

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
311 có thể được sử dụng để phân tách các thành phần

bàn số 3. Ví dụ về các từ viết tắt hợp lệTùy chọn hoặc Tiểu ban Mẫu Các từ viết tắt được công nhận

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
315

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
316,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
317,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
318 (…)

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
319

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
320,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
321,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
322,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
323,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
324,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
325 (…)

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
326

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
327,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
328,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
329,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
330,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
331 (…)

11. 2. 2. Từ viết tắt mơ hồ

Khi người dùng chỉ định đầu vào có thể khớp với nhiều tùy chọn hoặc tiểu ban, trình phân tích cú pháp sẽ đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244. Khi ứng dụng sử dụng phương pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154, người dùng sẽ nhận được thông báo lỗi và hướng dẫn sử dụng

Ví dụ: đưa ra một lệnh có các tiểu ban

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
334 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
335, thì đầu vào của người dùng không rõ ràng như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
336 sẽ hiển thị thông báo lỗi này

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
51

11. 2. 3. Tùy chọn dài viết tắt và Tùy chọn ngắn nhóm POSIX

Khi một đối số có thể khớp với cả tùy chọn dài viết tắt và một tập hợp , picocli khớp với tùy chọn dài. Đây là trường hợp không phụ thuộc vào chữ viết tắt, nhưng hãy lưu ý rằng khi bật các tùy chọn viết tắt, khả năng xảy ra các va chạm như vậy sẽ tăng lên

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
52

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
53

Khi các tùy chọn viết tắt được bật, đầu vào của người dùng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
337 sẽ khớp với tùy chọn dài
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
338, nhưng không khớp với các tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
339 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
340

11. 3. Ghi đè các tùy chọn đơn

Khi một tùy chọn một giá trị được chỉ định nhiều lần trên dòng lệnh, hành vi của trình phân tích cú pháp mặc định là đưa ra một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
341. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
54

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
55

Kết quả đầu vào sau đây trong một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
341

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
56

Các ứng dụng có thể thay đổi điều này bằng cách gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
343 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 trước khi phân tích cú pháp đầu vào. Khi các tùy chọn ghi đè được cho phép, giá trị được chỉ định cuối cùng sẽ có hiệu lực (đầu vào ở trên sẽ đặt trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
345 thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
346) và thông báo mức WARN được in ra bảng điều khiển. (Xem cách tắt cảnh báo. )

11. 4. Dừng lại ở vị trí

Theo mặc định, các tham số vị trí có thể được trộn lẫn với các tùy chọn trên dòng lệnh, nhưng điều này không phải lúc nào cũng mong muốn. Từ picocli 2. 3, các ứng dụng có thể gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
347 cùng với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 để buộc trình phân tích cú pháp coi tất cả các giá trị theo sau tham số vị trí đầu tiên là tham số vị trí

Khi cờ này được đặt, tham số vị trí đầu tiên sẽ đóng vai trò là điểm đánh dấu "" một cách hiệu quả

11. 5. Dừng lại ở mức chưa từng có

Từ picocli 2. 3, các ứng dụng có thể gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
349 cùng với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 để buộc trình phân tích cú pháp ngừng diễn giải các tùy chọn và tham số vị trí ngay khi gặp đối số chưa khớp

Khi cờ này được đặt, đối số chưa khớp đầu tiên và tất cả các đối số dòng lệnh tiếp theo sẽ được thêm vào danh sách đối số chưa khớp được trả về bởi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
351

11. 6. Đầu vào chưa khớp

Theo mặc định, một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352 được ném khi không thể gán đối số dòng lệnh cho một tùy chọn hoặc tham số vị trí. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
57

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
58

Lệnh chỉ có một trường được chú thích,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
353 và nó mong đợi chính xác ba đối số, do đó, kết quả đầu vào sau đây sẽ dẫn đến một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
59

Các ứng dụng có thể thay đổi điều này bằng cách gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
355 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 trước khi phân tích cú pháp đầu vào. Khi các đối số chưa khớp được cho phép, đầu vào ở trên sẽ được chấp nhận và thông báo mức WARN được in ra bảng điều khiển. (Xem cách tắt cảnh báo. )

Có thể thu được các giá trị đối số chưa khớp bằng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
351

11. 7. chú thích info.picocli picocli 4.7.0 87

kể từ picocli 3. 0, các trường được chú thích bằng


  info.picocli
  picocli
  4.7.0
87 sẽ được điền bằng các đối số chưa khớp. Trường phải thuộc loại
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
360 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
361

Nếu picocli tìm thấy một trường được chú thích bằng


  info.picocli
  picocli
  4.7.0
87, nó sẽ tự động đặt
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
363 thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 để không có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352 nào được ném ra khi không thể gán đối số dòng lệnh cho một tùy chọn hoặc tham số vị trí. Nếu không tìm thấy đối số chưa khớp, giá trị của trường được chú thích bằng

  info.picocli
  picocli
  4.7.0
87 sẽ không thay đổi

11. 8. Tùy chọn không xác định

11. 8. 1. Định nghĩa tùy chọn không xác định

Một trường hợp đặc biệt của đầu vào không khớp là các đối số giống với các tùy chọn nhưng không khớp với bất kỳ tùy chọn nào đã xác định. Picocli xác định xem một giá trị có "giống một tùy chọn" hay không bằng cách so sánh các ký tự đầu của nó với các ký tự tiền tố của các tùy chọn đã biết

Số âm không được coi là tùy chọn không xác định, vì vậy các giá trị như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
367,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
368,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
369,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
370 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
371 sẽ không được xử lý đặc biệt vì giống với tên tùy chọn

Ví dụ: giá trị

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
372 được coi là tùy chọn không xác định khi chúng tôi có lệnh chỉ xác định tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
020

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
60

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
61

Ở trên xác định các tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
020, nhưng không có tùy chọn nào
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
372. Vậy trình phân tích cú pháp nên làm gì khi người dùng cung cấp đầu vào như thế này?

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
62

11. 8. 2. Tham số Vị trí Tương tự Tùy chọn

Một khả năng là âm thầm chấp nhận các giá trị như tham số vị trí, nhưng điều này thường không được mong muốn

Theo mặc định, khi giá trị giống với một tùy chọn, picocli sẽ ném một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352 thay vì coi nó như một tham số vị trí

Picocli 3. 0 đã giới thiệu phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
379 có thể được sử dụng để buộc trình phân tích cú pháp xử lý các đối số giống như một tùy chọn làm tham số vị trí. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
62

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
380 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, tùy chọn không xác định
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
372 được coi là tham số vị trí. Đối số tiếp theo
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 được nhận dạng và xử lý như một tùy chọn đã biết như bạn mong đợi

Một cách khác là gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
355 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, điều này sẽ chấp nhận và lưu trữ các giá trị đó một cách riêng biệt như được mô tả trong

11. 8. 3. Tham số tùy chọn Tương tự như tùy chọn

Theo mặc định, các tùy chọn chấp nhận các giá trị tham số "giống" (nhưng không khớp chính xác) một tùy chọn

Picocli 4. 4 đã giới thiệu một phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
386 giúp có thể định cấu hình trình phân tích cú pháp để từ chối các giá trị giống với các tùy chọn làm tham số tùy chọn. Đặt giá trị này thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 sẽ dẫn đến các giá trị giống như tên tùy chọn bị từ chối làm giá trị tùy chọn

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
64

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
65

Theo mặc định, một giá trị như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
372, tương tự như một tùy chọn, được chấp nhận làm tham số cho
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
66

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
67

Sau khi đặt tùy chọn trình phân tích cú pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
390 thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247, các giá trị giống như một tùy chọn bị từ chối làm tham số cho
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
68

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
69

Điều này sẽ ném một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352 với tin nhắn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
70

11. 9. Tên tùy chọn hoặc tiểu ban dưới dạng giá trị tùy chọn

11. 9. 1. Theo các tùy chọn mặc định Không sử dụng các tên hoặc tiểu ban tùy chọn

kể từ picocli 4. 4, trình phân tích cú pháp sẽ không còn gán các giá trị khớp với tên tiểu ban hoặc tên tùy chọn cho các tùy chọn nhận tham số, trừ khi giá trị nằm trong dấu ngoặc kép. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
71

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
72

Trong các phiên bản trước của picocli, lệnh trên sẽ chấp nhận đầu vào

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
394 và giá trị
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
020 sẽ được gán cho trường Chuỗi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
246. Kể từ picocli 4. 4, đầu vào ở trên sẽ bị từ chối với thông báo lỗi cho biết rằng tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 yêu cầu tham số

Nếu cần phải chấp nhận các giá trị khớp với tên tùy chọn, các giá trị đó cần được trích dẫn. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
73

Điều này sẽ in đầu ra sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
74

11. 9. 2. Kích hoạt tiêu thụ tên tùy chọn hoặc tiểu ban

Picocli 4. 7. 0 giới thiệu hai tùy chọn cấu hình trình phân tích cú pháp để thay đổi hành vi này

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    398 cho phép tùy chọn sử dụng tên tùy chọn

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    399 cho phép các tùy chọn sử dụng tên lệnh con

Khi được đặt thành

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, tất cả các tùy chọn trong lệnh (các tùy chọn nhận tham số) có thể sử dụng các giá trị khớp với tên tùy chọn hoặc tên lệnh con

Điều này có nghĩa là bất kỳ tùy chọn nào cũng sẽ sử dụng số lượng đối số tối đa có thể cho tùy chọn đó.

SỬ DỤNG CẨN THẬN

Nếu một tùy chọn được định nghĩa là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
401, thì tùy chọn này sẽ sử dụng tất cả các đối số dòng lệnh còn lại sau tùy chọn này (cho đến ) làm tham số của tùy chọn này

11. 9. 3. Phân tích cú pháp tùy chỉnh cho hành vi tùy chọn cụ thể

Các tùy chọn cấu hình trình phân tích cú pháp trong phần trước áp dụng cho tất cả các tùy chọn trong lệnh

Một số ứng dụng có thể muốn bật các tùy chọn sử dụng tên tùy chọn hoặc tiểu ban cho một số tùy chọn, nhưng không phải cho tất cả các tùy chọn trong lệnh. Các ứng dụng như vậy có thể thay thế hoặc tăng cường trình phân tích cú pháp của picocli bằng cách thực hiện các tùy chọn đó. Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
75

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
76

Đoạn mã trên gán bất kỳ đối số dòng lệnh nào theo sau tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 cho tùy chọn đó và cho phép đầu vào như sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
77

11. 10. Chuyển đổi cờ Boolean

Khi một tùy chọn cờ được chỉ định trên dòng lệnh, picocli sẽ đặt giá trị của nó ngược lại với giá trị mặc định của nó

Trước 4. 0, mặc định là "chuyển đổi" các cờ boolean ngược lại với giá trị hiện tại của chúng. nếu giá trị trước đó là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 thì nó được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 và khi giá trị là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 thì nó được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248

Các ứng dụng có thể gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
407 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 để kích hoạt chuyển đổi. Lưu ý rằng khi bật bật tắt, việc chỉ định tùy chọn cờ hai lần trên dòng lệnh sẽ không có tác dụng vì chúng triệt tiêu lẫn nhau

11. 11. Tùy chọn ngắn nhóm POSIX

Theo mặc định, trình phân tích cú pháp picocli cho phép các tùy chọn ngắn được nhóm POSIX, do đó, các tùy chọn ngắn như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
409 có thể được nhóm lại với nhau như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
410. Từ picocli 3. 0, các ứng dụng có thể gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
411 với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 để thực thi rằng các tùy chọn phải được phân tách bằng khoảng trắng trên dòng lệnh. (Điều này cũng có nghĩa là các tham số tùy chọn phải được phân tách khỏi tên tùy chọn bằng khoảng trắng hoặc ký tự
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220, do đó,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
414 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
415 sẽ được nhận dạng nhưng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
214 thì không. )

11. 12. Chế độ khoan hồng

Từ picocli 3. 2, trình phân tích cú pháp có thể được định cấu hình để tiếp tục phân tích cú pháp đầu vào không hợp lệ đến cuối. Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
417 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 và xảy ra sự cố trong quá trình phân tích cú pháp, một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
419 được thêm vào danh sách được trả về bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
420 và quá trình phân tích cú pháp tiếp tục. Hành vi mặc định (khi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
417 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247) là hủy bỏ phân tích cú pháp bằng cách ném
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
419

Điều này hữu ích khi tạo các ứng cử viên hoàn thành trên đầu vào một phần và cũng hữu ích khi sử dụng picocli trong các ngôn ngữ như Clojure khi xử lý lỗi thành ngữ không liên quan đến việc ném và bắt ngoại lệ

Khi sử dụng tính năng này, các ứng dụng có trách nhiệm chủ động xác minh rằng không có lỗi nào xảy ra trước khi thực thi logic nghiệp vụ. Sử dụng cẩn thận

11. 13. Giá trị trích dẫn

11. 13. 1. báo giá cắt tỉa

Từ picocli 3. 7, các trích dẫn xung quanh các tham số dòng lệnh được giữ nguyên theo mặc định (trước đây chúng đã bị xóa). Điều này có thể được cấu hình với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
424 hoặc cấu hình trình phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425. Từ picocli 4. 0, các đối số được trích dẫn có thể chứa các chuỗi con được trích dẫn lồng nhau, để cung cấp cho người dùng cuối quyền kiểm soát chi tiết về cách các giá trị được phân chia

Nếu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
424 hoặc cấu hình trình phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, picocli sẽ xóa dấu ngoặc kép khỏi đối số dòng lệnh, như sau

  • Khi mỗi đối số dòng lệnh được xử lý, quy trình dưới đây được sử dụng để cắt bớt các trích dẫn ngoài cùng

  • Tiếp theo, nếu tùy chọn hoặc tham số vị trí có biểu thức chính quy

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    212 được xác định, thì giá trị tham số được phân tách trong khi tôn trọng dấu ngoặc kép. biểu thức chính quy
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    212 không khớp nếu nó xuất hiện trong chuỗi con được trích dẫn của giá trị tham số. Mỗi phần được tìm thấy trong quá trình phân tách sẽ bị xóa dấu ngoặc kép bằng cách sử dụng quy trình "bỏ trích dẫn thông minh" bên dưới

Xem phần bên dưới để biết ví dụ

Bỏ trích dẫn thông minh

  • Nếu đối số dòng lệnh chỉ chứa trích dẫn đầu và cuối, các trích dẫn này sẽ bị xóa

  • Nếu đối số dòng lệnh chứa dấu ngoặc kép không thoát, ngoại trừ dấu ngoặc kép đầu và cuối, thì đối số sẽ không thay đổi (dấu ngoặc kép đầu và cuối vẫn còn)

  • Nếu một đối số dòng lệnh được trích dẫn chứa dấu ngoặc kép thoát dấu gạch chéo ngược, thì dấu ngoặc kép ở đầu và cuối sẽ bị xóa, dấu ngoặc kép thoát dấu gạch chéo ngược được chuyển đổi thành dấu ngoặc kép không thoát và dấu gạch chéo ngược thoát dấu gạch chéo ngược được chuyển đổi thành dấu gạch chéo ngược không thoát

Ví dụ

Đối số dòng lệnh sau khi cắt trích dẫn Lưu ý

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
431

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
432

đã xóa dấu ngoặc kép

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
433

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
433

không thay đổi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
435

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
436

Tách ra sẽ tìm được 4 giá trị.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
437;

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
441

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
442

Tách ra sẽ tìm được 1 giá trị.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
443

11. 13. 2. Tách các tham số được trích dẫn

Theo mặc định, nếu tùy chọn hoặc tham số vị trí có biểu thức chính quy được xác định, giá trị tham số được chia thành các phần trong khi tôn trọng dấu ngoặc kép. biểu thức chính quy

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 không khớp trong vùng được trích dẫn

Thí dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
78

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
79

Đưa ra đầu vào như dưới đây

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
80

Điều này dẫn đến mảng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
446 có các giá trị sau, giả sử cấu hình trình phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247 (mặc định)

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
81

Nếu cấu hình trình phân tích cú pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, thì ví dụ trên sẽ được chia thành các giá trị sau (với dấu ngoặc kép được cắt bớt từ các phần kết quả)

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
82

Đưa ra đầu vào như dưới đây

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
83

Điều này dẫn đến mảng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
446 có các giá trị sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
84

Hoặc, nếu cấu hình trình phân tích cú pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
85

Để duy trì các trích dẫn khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, hãy chỉ định các trích dẫn lồng nhau bổ sung trên dòng lệnh. Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
86

Với cấu hình trình phân tích cú pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
425 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, đầu vào ở trên cung cấp các giá trị sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
87

Có thể tắt tính năng "phân tách thông minh" (tôn trọng dấu ngoặc kép) này bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
458. đặt thuộc tính trình phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
459 thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 sẽ tắt tính năng chia tách thông minh và biểu thức chính quy
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 được áp dụng cho giá trị tham số bất kể dấu ngoặc kép

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
459 chủ yếu dành cho khả năng tương thích ngược, dành cho các ứng dụng muốn phiên bản trước 3. 7 hành vi chia tách đơn giản bất kể dấu ngoặc kép. Hầu hết các ứng dụng nên để cài đặt này về mặc định (
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247). Khi cài đặt này là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, đầu vào ở trên được phân tích thành

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
88

11. 14. Tùy chỉnh các tùy chọn có thể phủ định

có thể được tùy chỉnh thông qua giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
465

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
89

Điều này cho phép bạn kiểm soát

  • tên tùy chọn nào nên có dạng phủ định

  • dạng phủ định được trình phân tích cú pháp nhận ra trong khi phân tích cú pháp dòng lệnh

  • chuỗi tài liệu hiển thị cả dạng khẳng định và phủ định trong thông báo trợ giúp sử dụng

Theo mặc định, một bộ được sử dụng để kiểm soát ở trên. Sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
466 để thay thế
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
465 bằng phiên bản tùy chỉnh. Xem JavaDoc để biết chi tiết

11. 15. Xử lý tham số tùy chỉnh

Kể từ phiên bản 4. 6, picocli cung cấp hai plugin trình phân tích cú pháp khác nhau để xử lý tham số tùy chỉnh. trong khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468 là một công cụ mạnh mẽ và linh hoạt, thì
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243 đóng vai trò là một công cụ thay thế đơn giản hơn

11. 15. 1. Trình cắm phân tích cú pháp
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243

Các tùy chọn hoặc tham số vị trí có thể được chỉ định một

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243 thực hiện logic tùy chỉnh để xử lý các tham số cho tùy chọn này hoặc vị trí này. Khi một tùy chọn hoặc tham số vị trí với một tùy chỉnh
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243 được khớp trên dòng lệnh, trình phân tích cú pháp nội bộ của picocli tạm thời bị treo và người tiêu dùng tham số tùy chỉnh sẽ chịu trách nhiệm sử dụng và xử lý nhiều đối số dòng lệnh nếu cần

Điều này có thể hữu ích khi chuyển các tùy chọn sang lệnh khác

Ví dụ: lệnh unix

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
473 có tùy chọn thực hiện một số hành động cho mỗi tệp được tìm thấy. Bất kỳ đối số nào theo sau tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
474 cho đến đối số
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
476 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
477 đều không phải là tùy chọn cho chính lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
473, nhưng được hiểu là một lệnh riêng biệt và các tùy chọn của nó

Ví dụ dưới đây minh họa cách triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
479 bằng API này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
90

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
91

Đảm bảo rằng mọi lớp lồng nhau là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
781, nếu không picocli sẽ không thể khởi tạo chúng

11. 15. 2. Trình cắm phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468

Được giới thiệu trong picocli 4. 6,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468 cũng là một plugin trình phân tích cú pháp, tương tự như
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243, nhưng linh hoạt hơn

Các tùy chọn, tham số vị trí và lệnh có thể được gán một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468 thực hiện logic tùy chỉnh để xử lý trước các tham số cho tùy chọn, vị trí hoặc lệnh này. Khi một tùy chọn, tham số vị trí hoặc lệnh có tùy chỉnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468 được khớp trên dòng lệnh, trình phân tích cú pháp nội bộ của picocli tạm thời bị treo và logic tùy chỉnh này được gọi

Logic tùy chỉnh này có thể thay thế hoàn toàn phân tích cú pháp nội bộ của picocli cho tùy chọn, tham số vị trí hoặc lệnh này hoặc bổ sung nó bằng cách thực hiện một số xử lý trước khi phân tích cú pháp nội bộ của picocli được tiếp tục cho tùy chọn, tham số vị trí hoặc lệnh này

Các hành động "tiền xử lý" có thể bao gồm sửa đổi chồng tham số dòng lệnh hoặc sửa đổi mô hình

Trường hợp sử dụng ví dụ

Điều này có thể hữu ích khi định hướng đầu vào cho các lệnh có cả tham số vị trí và tùy chọn có tham số tùy chọn. Ví dụ: giả sử chúng ta có một lệnh với nội dung tóm tắt sau

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
92

Một trong số đó là chúng khó kết hợp với các tham số vị trí

Với plugin trình phân tích cú pháp tùy chỉnh, chúng tôi có thể tùy chỉnh trình phân tích cú pháp, chẳng hạn như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
486 trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
487 được hiểu là tham số tùy chọn và trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
488 (không có dấu tách
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220), VALUE được hiểu là tham số vị trí. Đoạn mã dưới đây chứng minh điều này

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
93

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
94

Đảm bảo rằng mọi lớp lồng nhau là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
781, nếu không picocli sẽ không thể khởi tạo chúng

Với bộ tiền xử lý này, giờ đây người dùng có thể chỉ định trình chỉnh sửa của mình (e. g.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
491). Nếu không có trình chỉnh sửa nào được cung cấp, trình chỉnh sửa mặc định sẽ được sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
95

11. 15. 3. So sánh trình cắm phân tích cú pháp

Bảng 4. So sánh các plugin trình phân tích cú pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468 và
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
468
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243

Tóm lược

Tăng cường (trả lại

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247) hoặc thay thế (trả lại
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248) logic phân tích cú pháp picocli cho phần tử phù hợp

Thay thế logic phân tích cú pháp picocli cho phần tử phù hợp

Phạm vi

Các lệnh cũng như các tùy chọn và tham số vị trí

Chỉ các tùy chọn và tham số vị trí

Đọc trạng thái trình phân tích cú pháp

Có, thông tin có thể được nhận qua bản đồ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
498

Không

Sửa đổi trạng thái trình phân tích cú pháp

Có, thông qua sửa đổi bản đồ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
498

Không

12. Giúp đỡ

12. 1. Tùy chọn trợ giúp

Các ứng dụng có thể xác định các tùy chọn trợ giúp bằng cách đặt thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
500,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
501 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
502. Nếu một trong các đối số được chỉ định trên dòng lệnh là tùy chọn "trợ giúp", picocli sẽ không đưa ra một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
010 khi thiếu các tùy chọn bắt buộc

Ví dụ

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
96

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
97

Sử dụng các thuộc tính này cho các tùy chọn yêu cầu thông báo trợ giúp sử dụng hoặc thông tin phiên bản được hiển thị trên bảng điều khiển

Java

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
98

Kotlin

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
99

Lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 cung cấp hai phương thức cho phép các thành phần bên ngoài phát hiện xem thông tin phiên bản hoặc trợ giúp sử dụng có được yêu cầu hay không (không cần kiểm tra đối tượng miền được chú thích)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    505 trả về
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    248 nếu trình phân tích cú pháp khớp với một tùy chọn được chú thích bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    507

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    508 trả về
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    248 nếu trình phân tích cú pháp khớp với một tùy chọn được chú thích bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    510

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
00

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
01

Xem thêm

12. 2. Tùy chọn Trợ giúp Tiêu chuẩn Mixin

Picocli 3. 0 đã giới thiệu thuộc tính lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
511. Khi thuộc tính này được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248, picocli sẽ thêm a vào lệnh thêm và tùy chọn vào lệnh. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
02

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
03

Các lệnh với

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
511 không cần khai báo rõ ràng các trường được chú thích bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
516 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
517 nữa. Thông báo trợ giúp sử dụng cho ví dụ trên trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
04

12. 3. Tiểu ban trợ giúp tích hợp

Kể từ phiên bản 3. 0, picocli cung cấp lệnh phụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
334 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
519) có thể được cài đặt dưới dạng lệnh phụ trên bất kỳ lệnh ứng dụng nào. Nó in trợ giúp sử dụng cho lệnh cha hoặc lệnh phụ anh chị em. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
05

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
06

Ví dụ: lệnh sau in trợ giúp sử dụng cho một lệnh con

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
07

Để in trợ giúp sử dụng cho lệnh chính

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
08

12. 4. Tiểu ban trợ giúp tùy chỉnh

Các tiểu ban trợ giúp tùy chỉnh nên tự đánh dấu là a để yêu cầu picocli không ném

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
010 khi thiếu các tùy chọn bắt buộc

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
09

Picocli 4. 0 đã giới thiệu một giao diện mới

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
521 cung cấp các lệnh trợ giúp tùy chỉnh với quyền truy cập vào lệnh cha và các lệnh anh chị em, có sử dụng màu Ansi hay không và các luồng để in thông báo trợ giúp sử dụng tới

Giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
522 thay thế giao diện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
523 ​​đã được giới thiệu trong picocli 3. 0

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
10

12. 5. Hỗ trợ in tự động

kể từ picocli 2. 0, sẽ tự động in thông tin phiên bản và trợ giúp sử dụng khi tùy chọn trợ giúp được chỉ định trên dòng lệnh (các tùy chọn được chú thích bằng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
514 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
513 - nhưng không phải thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
334)

Điều tương tự cũng áp dụng cho thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
511,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
528 tích hợp sẵn và bất kỳ lệnh con trợ giúp tùy chỉnh nào được đánh dấu là

Trợ giúp in tự động sau đây

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    253

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    530

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    531

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    532

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    533 (với trình xử lý
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    534 tích hợp sẵn)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    535 (với trình xử lý
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    534 tích hợp sẵn)

Các phương pháp sau đây không tự động in trợ giúp

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    537

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    255

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    539

Khi sử dụng ba phương pháp cuối cùng, các ứng dụng cần truy vấn kết quả phân tích cú pháp để phát hiện xem trợ giúp sử dụng hoặc trợ giúp phiên bản có được yêu cầu hay không và gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
540 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
541 để thực sự in thông báo trợ giúp được yêu cầu

13. Phiên bản Trợ giúp

13. 1. Thông tin phiên bản tĩnh

13. 1. 1. Thuộc tính lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
542

kể từ picocli 0. 9. 8, các ứng dụng có thể chỉ định thông tin phiên bản trong thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
542 của chú thích

  info.picocli
  picocli
  4.7.0
91

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
11

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
12

Phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
545 trích xuất thông tin phiên bản từ chú thích này và in nó sang
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
546 đã chỉ định

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
13

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
14

13. 1. 2. Thông tin phiên bản nhiều dòng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
542 có thể chỉ định nhiều Chuỗi. Mỗi cái sẽ được in trên một dòng riêng biệt

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
15

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
16

Phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
545 sẽ in ở trên dưới dạng

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
17

13. 1. 3. Thông tin phiên bản có biến

Kể từ picocli 4. 0, chuỗi phiên bản có thể chứa. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
18

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
19

Tùy thuộc vào môi trường của bạn, điều đó có thể in một cái gì đó như

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
20

13. 1. 4. Thông tin phiên bản có màu

Các chuỗi phiên bản có thể chứa để hiển thị kiểu và màu ANSI. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
21

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
22

Đánh dấu sẽ được hiển thị dưới dạng mã thoát ANSI trên các hệ thống được hỗ trợ

Python chuyển sang ansi

13. 1. 5. Thông tin Phiên bản Với Định dạng Định dạng

kể từ picocli 1. 0,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
542 có thể chứa

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
23

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
24

Các giá trị đối số định dạng có thể được chuyển đến phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
550

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
25

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
26

13. 2. Thông tin phiên bản động

13. 2. 1. Thuộc tính lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
551

kể từ picocli 2. 2, chú thích


  info.picocli
  picocli
  4.7.0
91 hỗ trợ thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
551. Các ứng dụng có thể chỉ định triển khai
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554 trong thuộc tính này và picocli sẽ khởi tạo lớp này và gọi nó để thu thập thông tin phiên bản

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
27

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
28

Điều này hữu ích khi phiên bản của ứng dụng sẽ được phát hiện động khi chạy. Ví dụ: một triển khai có thể trả về thông tin phiên bản thu được từ tệp kê khai JAR, tệp thuộc tính hoặc một số nguồn khác

13. 2. 2. Giao diện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554

Nhà cung cấp phiên bản tùy chỉnh cần triển khai giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
556

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
29

Các nhà cung cấp phiên bản được khai báo với thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
551 cần phải có một hàm tạo không có đối số công khai để được khởi tạo, trừ khi a được cài đặt để khởi tạo các lớp

Dự án GitHub có một ví dụ dựa trên tệp kê khai và triển khai nhà cung cấp phiên bản dựa trên tệp thuộc tính phiên bản do phiên bản tạo

13. 2. 3. Thông tin phiên bản động với các biến

Các chuỗi phiên bản được trả về từ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554 có thể chứa

Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
30

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
31

Nhà cung cấp phiên bản mẫu ở trên sẽ hiển thị tên lệnh đủ điều kiện (nghĩa là trước tên lệnh mẹ đủ điều kiện) của bất kỳ lệnh nào sử dụng nhà cung cấp phiên bản này

Đây là một cách để tạo nhà cung cấp phiên bản có thể được sử dụng lại trên nhiều lệnh

13. 2. 4. Tiêm
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 vào một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554

Kể từ picocli 4. 2. 0, triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554 có thể có các trường được chú thích

  info.picocli
  picocli
  4.7.0
92. Nếu một trường như vậy tồn tại, picocli sẽ đưa vào
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 của lệnh sử dụng nhà cung cấp phiên bản này. Điều này cho phép nhà cung cấp phiên bản truy cập vào hệ thống phân cấp lệnh đầy đủ và có thể giúp dễ dàng triển khai các nhà cung cấp phiên bản có thể được sử dụng lại giữa nhiều lệnh

Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
32

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
33

14. Trợ giúp sử dụng

14. 1. Ví dụ rút gọn

Thông báo trợ giúp sử dụng picocli mặc định trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
34

Thông báo trợ giúp sử dụng được tạo từ các thuộc tính chú thích, như bên dưới

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
35

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
36

14. 2. Tên lệnh

Trong ví dụ trên, tên chương trình được lấy từ thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
564 của chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
565

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
37

Nếu không có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
564, picocli sẽ hiển thị một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
567 chung trong phần tóm tắt

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
38

14. 3. Nhãn tham số

Các tùy chọn không phải boolean yêu cầu một giá trị. Trợ giúp sử dụng sẽ giải thích điều này và picocli hiển thị tham số tùy chọn trong bản tóm tắt và trong danh sách tùy chọn. Theo mặc định, tên trường được hiển thị trong dấu ngoặc đơn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
568 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
569. Sử dụng thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
570 để hiển thị một tên khác. Ví dụ

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
39

Một số trường được chú thích trong lớp ví dụ bên dưới có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
570 và những trường khác thì không

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
40

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
41

Đối với mục đích trình diễn, ví dụ trên kết hợp tất cả các chữ hoa (e. g. ,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
572) nhãn kiểu và khung cá (e. g. ,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
573) nhãn kiểu. Đối với các ứng dụng thực tế, nên tránh trộn lẫn các kiểu nhãn này. Một ứng dụng chỉ nên sử dụng một kiểu nhất quán

14. 4. Danh sách tùy chọn

14. 4. 1. Danh sách tùy chọn được sắp xếp

Theo mặc định, danh sách tùy chọn trong thông báo trợ giúp sử dụng hiển thị các tùy chọn theo thứ tự bảng chữ cái. Sử dụng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
574 để hiển thị các tùy chọn theo thứ tự chúng được khai báo trong lớp của bạn

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
42

Việc sắp xếp theo thứ tự khai báo được thực hiện trên cơ sở cố gắng hết sức, xem phần bên dưới

14. 4. 2. Tùy chọn sắp xếp lại

Lưu ý rằng picocli không thể phát hiện thứ tự khai báo một cách đáng tin cậy trong các lệnh có cả phương thức được chú thích bởi


  info.picocli
  picocli
  4.7.0
89 và trường được chú thích bởi

  info.picocli
  picocli
  4.7.0
89

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
577 có thể được sử dụng để kiểm soát rõ ràng vị trí trong thông báo trợ giúp sử dụng mà tại đó tùy chọn sẽ được hiển thị. Các tùy chọn có số thấp hơn được hiển thị trước các tùy chọn có số cao hơn

14. 4. 3. Sắp xếp tóm tắt

Theo mặc định, phần tóm tắt thông báo trợ giúp sử dụng hiển thị các tùy chọn theo thứ tự bảng chữ cái. Picocli 4. 7. 0 đã giới thiệu thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
578 để cho phép các tùy chọn hiển thị tóm tắt theo thứ tự chúng được khai báo trong lớp của bạn hoặc được sắp xếp theo thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
108 của chúng

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
43

Bất kể chiến lược sắp xếp là gì, các tùy chọn boolean short được hiển thị đầu tiên dưới dạng một nhóm duy nhất trong bản tóm tắt, tiếp theo là các tùy chọn lấy tham số, trừ khi trình phân tích cú pháp không cho phép các tùy chọn boolean short được nhóm

14. 4. 4. Điểm đánh dấu tùy chọn bắt buộc

Các tùy chọn bắt buộc có thể được đánh dấu trong danh sách tùy chọn bằng ký tự được chỉ định bằng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
580. Theo mặc định, các tùy chọn không được đánh dấu vì bản tóm tắt hiển thị cho người dùng tùy chọn nào là bắt buộc và tùy chọn nào là tùy chọn. Tính năng này có thể hữu ích khi kết hợp với thuộc tính. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
44

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
45

Tạo thông báo trợ giúp sử dụng sau

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
46

14. 4. 5. Các cột tùy chọn ngắn và dài

Bố cục mặc định hiển thị các tùy chọn ngắn và tùy chọn dài trong các cột riêng biệt, theo sau là cột mô tả

Chỉ được hiển thị trong cột đầu tiên, nghĩa là các tùy chọn bắt đầu bằng một dấu gạch ngang

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
311 và dài một ký tự

Tùy chọn có hai ký tự không được coi là tùy chọn ngắn và được hiển thị trong cột tùy chọn dài

Đây là bố cục phổ biến cho các tiện ích Unix và có thể nâng cao khả năng sử dụng. các ứng dụng có thể đưa ra gợi ý tinh tế cho người dùng cuối rằng một tùy chọn phổ biến và được khuyến khích bằng cách cung cấp cả tên ngắn và tên dài cho tùy chọn. Ngược lại, việc không có quyền chọn bán khống có thể báo hiệu rằng quyền chọn này không bình thường hoặc có lẽ nên được sử dụng cẩn thận

Có thể căn trái tất cả các tùy chọn bằng cách sử dụng bố cục tùy chỉnh. Xem LeftAlignOptions. java trong mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 để biết ví dụ

14. 4. 6. Chiều rộng cột tùy chọn dài

Bố cục mặc định hiển thị các tùy chọn ngắn và tùy chọn dài trong các cột riêng biệt, theo sau là cột mô tả. Chiều rộng của cột tùy chọn dài tự động co lại nếu tất cả các tùy chọn dài đều rất ngắn, nhưng theo mặc định, cột này không lớn hơn 20 ký tự

Nếu tùy chọn dài với tham số tùy chọn của nó dài hơn 20 ký tự (ví dụ:.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
584), tùy chọn dài tràn vào cột mô tả và mô tả tùy chọn được hiển thị trên dòng tiếp theo

Cái này (mặc định) trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
47

Kể từ picocli 4. 2, có API lập trình để thay đổi điều này thông qua các phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
585 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
586

Trong ví dụ trên, nếu chúng ta gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
587 trước khi in trợ giúp sử dụng, chúng ta sẽ nhận được kết quả này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
48

Giá trị tối thiểu có thể được chỉ định cho

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
588 là 20, giá trị tối đa là âm 20

14. 5. Chiều rộng sử dụng

Độ rộng mặc định của thông báo trợ giúp sử dụng là 80 ký tự. Các lệnh được xác định bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
589 trong chú thích sẽ sử dụng chiều rộng đã chỉ định

Picocli 3. 0 cũng đã giới thiệu API có lập trình cho điều này thông qua các phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
590 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
591

Người dùng cuối có thể sử dụng thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
592 để chỉ định chiều rộng tùy chỉnh ghi đè giá trị được đặt theo chương trình

Chiều rộng tối thiểu có thể được cấu hình là 55 ký tự

14. 6. Chiều rộng tự động (thiết bị đầu cuối)

Kể từ picocli 4. 0, các lệnh được xác định bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
593 sẽ cố gắng điều chỉnh bố cục trợ giúp thông báo sử dụng theo chiều rộng của thiết bị đầu cuối. Ngoài ra còn có API lập trình để kiểm soát điều này thông qua các phương pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
594 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
595

Người dùng cuối có thể kích hoạt tính năng này bằng cách đặt thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
592 thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
597 và có thể tắt tính năng này bằng cách đặt thuộc tính hệ thống này thành một giá trị số

Tính năng này yêu cầu Java 7

14. 7. Tách nhãn tóm tắt

Các tùy chọn và tham số có thể có một thuộc tính để chia từng tham số thành các chuỗi con nhỏ hơn. Biểu thức chính quy có thể chứa văn bản bằng chữ, nhưng cũng có thể chứa các ký tự đặc biệt. Biểu thức chính quy trong thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 có thể khá khác so với những gì người dùng cuối cần nhập

Ví dụ bên dưới sử dụng ký tự dấu cộng (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
477) làm dấu phân cách. Biểu thức chính quy trong thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 sử dụng dấu gạch chéo ngược (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
602) ký tự để đảm bảo rằng ký tự dấu cộng được coi là ký tự chữ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
49

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
50

Tuy nhiên, người dùng cuối có thể chỉ cần nhập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
603 (sử dụng dấu phân cách ký tự cộng), chứ không phải
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
604. Chúng tôi muốn thông báo trợ giúp sử dụng hiển thị điều này

Thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
006, được giới thiệu trong picocli 4. 3, kiểm soát nội dung được hiển thị trong phần tóm tắt của thông báo trợ giúp sử dụng

Với ví dụ trên, phần tóm tắt trợ giúp sử dụng trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
51

14. 8. tóm tắt tóm tắt

Nếu một lệnh rất phức tạp và có nhiều tùy chọn, đôi khi nên loại bỏ các chi tiết khỏi phần tóm tắt bằng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
581. Ví dụ

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
52

Lưu ý rằng các tham số vị trí không được viết tắt

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
53

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
54

14. 9. Tóm tắt tùy chỉnh

Để kiểm soát nhiều hơn phần tóm tắt, hãy sử dụng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
607 để chỉ định một hoặc nhiều dòng tóm tắt. Ví dụ

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
55

Để tạo một bản tóm tắt như trên, hãy chỉ định văn bản bằng chữ trong thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
607

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
56

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
57

14. 10. Nội dung tóm tắt Nhãn tiểu ban

Đối với các lệnh có , chuỗi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
609 được thêm vào cuối phần tóm tắt (dù phần tóm tắt có hay không). Cái này trông giống như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
58

Kể từ picocli 4. 0, điều này có thể được tùy chỉnh với thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
610

Ví dụ: để làm rõ rằng một , ứng dụng có thể chỉ định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
611, không có dấu ngoặc kép
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
612 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
613

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
59

Ví dụ, một ứng dụng có số lượng tiểu ban hạn chế có thể muốn hiển thị tất cả chúng trong phần tóm tắt

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
60

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
61

Điều này sẽ hiển thị bản tóm tắt sau

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
62

14. 11. Đầu trang và cuối trang

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
614 sẽ được hiển thị ở đầu thông báo trợ giúp sử dụng (trước phần tóm tắt). Dòng tiêu đề đầu tiên cũng là dòng hiển thị trong danh sách lệnh con nếu lệnh của bạn có các lệnh con (xem phần )

Sử dụng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
615 để chỉ định một hoặc nhiều dòng hiển thị bên dưới thông báo trợ giúp sử dụng đã tạo

Mỗi phần tử của mảng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
717 được hiển thị trên một dòng riêng biệt

14. 12. Danh sách mã thoát

Theo mặc định, thông báo trợ giúp sử dụng không hiển thị thông tin. Các ứng dụng gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 cần định cấu hình các thuộc tính chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
198 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
199. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
63

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
64

Thao tác này sẽ in thông báo trợ giúp sử dụng sau tới bảng điều khiển

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
65

14. 13. Công cụ xác định định dạng

Tất cả các thành phần thông báo trợ giúp sử dụng có thể có các chỉ định định dạng dấu tách dòng (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
102) được nhúng. Chúng được chuyển đổi thành dấu tách dòng dành riêng cho nền tảng khi thông báo trợ giúp sử dụng được in

có thể có các bộ xác định định dạng định dạng các đối số bổ sung được truyền cho phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
550

xem java. sử dụng. Trình định dạng JavaDoc để biết chi tiết

Lưu ý rằng để hiển thị phần trăm

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
622 ký tự trong thông báo trợ giúp sử dụng, chúng cần được thoát bằng một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
623 khác. Ví dụ.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
624 được hiển thị dưới dạng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
625

Một cách khác để kiểm soát bố cục của thông báo trợ giúp sử dụng là một số phần (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
614,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
615 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261) có thể được chỉ định dưới dạng một mảng Chuỗi, trong đó mỗi phần tử của mảng được hiển thị trên một dòng riêng trong thông báo trợ giúp sử dụng

14. 14. Đề mục

Phần tiêu đề có thể được sử dụng để làm cho bố cục thông báo sử dụng trông rộng rãi hơn. Ví dụ dưới đây minh họa việc sử dụng bộ chỉ định định dạng dấu tách dòng nhúng (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
102)

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
66

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
67

Thông báo trợ giúp sử dụng được tạo từ lớp này được hiển thị bên dưới trong

14. 15. Ví dụ mở rộng

Ví dụ dưới đây minh họa thông báo sử dụng tùy chỉnh có thể trông như thế nào. Lưu ý cách các tiêu đề phần có dấu phân cách dòng có thể tạo thông báo sử dụng rộng rãi hơn và các tùy chọn cũng được liệt kê theo thứ tự khai báo (thay vì theo thứ tự bảng chữ cái)

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
68

Lớp chú thích mà thông báo trợ giúp sử dụng này được tạo ra từ đó được hiển thị trong

14. 16. Dấu tách tùy chọn-tham số

Dấu tách được hiển thị giữa các tùy chọn và tham số tùy chọn (

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220 theo mặc định) trong phần tóm tắt và danh sách tùy chọn có thể được định cấu hình bằng thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
631

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
69

Chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
632 cũng ảnh hưởng đến cách picocli phân tích cú pháp dòng lệnh. Xem thêm

14. 17. Các tùy chọn và tham số ẩn

Tùy chọn và Tham số với thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
633 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 sẽ không được hiển thị trong thông báo trợ giúp sử dụng. Ví dụ, điều này hữu ích khi một tham số tại một số chỉ mục được ghi vào nhiều trường. theo mặc định, mỗi trường này sẽ được hiển thị trong thông báo sử dụng, điều này sẽ gây nhầm lẫn cho người dùng

Ví dụ: trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
635 bên dưới được chú thích là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
636

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
70

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
71

Ở trên sẽ tạo thông báo trợ giúp sử dụng sau đây, trong đó trường

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
635 không được hiển thị

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
72

14. 18. Hiển thị tại tệp

Kể từ picocli 4. 2, một mục cho

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 có thể được hiển thị trong danh sách tùy chọn và tham số của thông báo trợ giúp sử dụng của một lệnh với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
709

14. 18. 1. Thí dụ

lệnh ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
73

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
74

Thông báo trợ giúp sử dụng cho lệnh này trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
75

14. 18. 2. Thay đổi vị trí tại mục nhập tệp

Theo mặc định, mục nhập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 được hiển thị trước các tham số vị trí trong phần tóm tắt cũng như trong danh sách tham số

Điều này có thể được thay đổi với các phần sắp xếp lại. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
76

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
77

Thông báo trợ giúp sử dụng kết quả cho thấy mục nhập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 đã được di chuyển xuống dưới cùng, theo sau danh sách tùy chọn

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
78

14. 18. 3. Thay đổi văn bản tại mục nhập tệp

Cả nhãn và mô tả của mục nhập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 đã được xác định bằng , để cho phép các ứng dụng thay đổi văn bản. Các biến là

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    643

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    644

Bằng cách đặt các biến trên trong thuộc tính hệ thống, biến môi trường hoặc lệnh for, văn bản có thể được tùy chỉnh

Ví dụ: nếu chúng ta xác định các thuộc tính hệ thống này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
79

sau đó trợ giúp sử dụng cho ví dụ trên của chúng tôi thay đổi thành này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
80

Mô tả của mục nhập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 cũng có thể được chỉ định trong phần quốc tế hóa và bản địa hóa.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 cho mục nhập
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708 trong gói tài nguyên là

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    648

14. 19. Hiển thị kết thúc tùy chọn

Kể từ picocli 4. 3, một mục nhập cho


  info.picocli
  picocli
  4.7.0
84 có thể được hiển thị trong danh sách tùy chọn của thông báo trợ giúp sử dụng của một lệnh với chú thích
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
285

14. 19. 1. Thí dụ

lệnh ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
81

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
82

Thông báo trợ giúp sử dụng cho lệnh này trông như thế này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
83

14. 19. 2. Thay đổi vị trí mục nhập kết thúc tùy chọn

Theo mặc định, mục nhập


  info.picocli
  picocli
  4.7.0
84 End of Options được hiển thị giữa các tùy chọn và tham số vị trí trong phần tóm tắt và ở cuối danh sách tùy chọn

Điều này có thể được thay đổi với các phần sắp xếp lại. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
84

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
85

Thông báo trợ giúp sử dụng kết quả hiển thị mục nhập


  info.picocli
  picocli
  4.7.0
84 End of Options đã được chuyển lên đầu danh sách tùy chọn

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
86

14. 19. 3. Thay đổi Văn bản Mục nhập Kết thúc Tùy chọn

Mô tả của mục nhập dấu phân cách Kết thúc Tùy chọn


  info.picocli
  picocli
  4.7.0
84 đã được xác định bằng dấu , để cho phép các ứng dụng thay đổi văn bản. biến là

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    654

Bằng cách đặt biến trên trong thuộc tính hệ thống, biến môi trường hoặc lệnh for, văn bản có thể được tùy chỉnh

Ví dụ: nếu chúng tôi xác định thuộc tính hệ thống này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
87

sau đó trợ giúp sử dụng cho ví dụ trên của chúng tôi thay đổi thành này

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
88

Mô tả của mục nhập dấu phân cách Kết thúc tùy chọn cũng có thể được chỉ định trong phần quốc tế hóa và bản địa hóa.

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 cho mục nhập dấu phân cách Kết thúc Tùy chọn trong các gói tài nguyên là

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    656

14. 20. Hiển thị giá trị mặc định

14. 20. 1. Biến
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260

Từ picocli 3. 2, có thể nhúng vào phần mô tả cho một tùy chọn hoặc tham số vị trí bằng cách chỉ định

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260 trong văn bản mô tả. Picocli sử dụng sự phản chiếu để lấy các giá trị mặc định từ các trường được chú thích

The được thay thế bằng giá trị mặc định bất kể thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
659 và bất kể thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
660 hay
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
661

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
89

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
90

Điều này tạo ra trợ giúp sử dụng sau đây

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
91

14. 20. 2.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
662 Biến

Tương tự, có thể nhúng các ứng cử viên hoàn thành trong mô tả cho một tùy chọn hoặc tham số vị trí bằng cách chỉ định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
662 trong văn bản mô tả

Điều này hoạt động cho các lớp java

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
716 và cho các tùy chọn hoặc tham số vị trí của các loại không liệt kê mà các ứng cử viên hoàn thành được chỉ định

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
92

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
93

Điều này tạo ra trợ giúp sử dụng sau đây

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
94

14. 20. 3. Cấu hình kế thừa để hiển thị giá trị mặc định

Trước picocli 3. 2, bạn cần sử dụng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
665 để nối thêm giá trị mặc định của tất cả các tùy chọn không phải ____1295 và tham số vị trí vào cột mô tả

Ngoài ra, picocli 3. 0 đã giới thiệu thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
667 cho chú thích

  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90. Điều này cho phép bạn chỉ định cho từng tùy chọn riêng lẻ và tham số vị trí liệu giá trị mặc định của nó có được hiển thị trong phần trợ giúp sử dụng hay không. Thuộc tính này chấp nhận ba giá trị

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    670 - luôn hiển thị giá trị mặc định của tùy chọn này hoặc tham số vị trí, thậm chí cả giá trị
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    295, bất kể giá trị nào của
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    672 được chỉ định trong lệnh

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    673 - không hiển thị giá trị mặc định cho tùy chọn này hoặc tham số vị trí, bất kể giá trị nào của
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    672 được chỉ định trong lệnh

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    675 - (đây là mặc định) chỉ hiển thị giá trị mặc định cho tùy chọn này hoặc tham số vị trí nếu
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    672 được chỉ định trong lệnh

Các cơ chế kế thừa này vẫn hoạt động nhưng để linh hoạt tối đa, hãy sử dụng các biến được giải thích ở trên

15. Màu sắc và kiểu dáng ANSI

Sử dụng màu sắc trong đầu ra lệnh của bạn không chỉ trông đẹp mắt. bằng cách đối chiếu các yếu tố quan trọng như tên tùy chọn với phần còn lại của thông báo, nó làm giảm tải nhận thức cho người dùng

15. 1. Ví dụ được tô màu

Bên dưới hiển thị thông báo trợ giúp sử dụng tương tự như trong, với mã thoát ANSI được bật

Python chuyển sang ansi

15. 2. Trợ giúp sử dụng với Kiểu dáng và Màu sắc

Bạn có thể sử dụng màu sắc và kiểu dáng trong phần mô tả, đầu trang và chân trang của thông báo trợ giúp sử dụng

Picocli hỗ trợ ký hiệu đánh dấu tùy chỉnh để trộn màu và kiểu trong văn bản, tuân theo quy ước do Jansi giới thiệu, trong đó

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
677 bắt đầu phần được tạo kiểu và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
678 kết thúc phần đó. Ngay sau
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
677 là danh sách màu sắc và kiểu cách được phân tách bằng dấu phẩy, vì vậy,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
680. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
95

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
96

Python chuyển sang ansi

Bảng 5. Các kiểu và màu được xác định trước có thể được sử dụng trong các mô tả và tiêu đề bằng cách sử dụng ký hiệu
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
680 Kiểu được xác định trước Màu được xác định trước

Dũng cảm

màu đen

mờ nhạt

màu đỏ

gạch dưới

màu xanh lá

chữ nghiêng

màu vàng

nháy mắt

màu xanh da trời

đảo ngược

đỏ tươi

cài lại

lục lam

trắng

Màu được áp dụng làm màu nền trước theo mặc định. Bạn có thể đặt màu nền bằng cách chỉ định

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
682. Ví dụ:
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
683. Tương tự,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
684 đặt màu nền trước một cách rõ ràng

Ví dụ dưới đây cho thấy cách đánh dấu này có thể được sử dụng để thêm màu sắc và kiểu dáng cho tiêu đề và mô tả của thông báo trợ giúp sử dụng

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
97

Kotlin

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
98

Kiểu đánh dấu không thể được lồng vào nhau, ví dụ.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
685 sẽ không hoạt động. Bạn có thể đạt được điều tương tự bằng cách kết hợp các kiểu, ví dụ.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
686 sẽ hoạt động tốt

Kể từ picocli 4. 2, đánh dấu tùy chỉnh như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
687,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
688, v.v. cũng có thể được chuyển đổi thành đánh dấu tùy chỉnh như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
689 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
690 trong HTML hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
691 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
692 trong các ngôn ngữ đánh dấu nhẹ như AsciiDoc. Các ứng dụng có thể kiểm soát điều này bằng cách đặt
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
693 với bản đồ đánh dấu tùy chỉnh. Tính năng này được sử dụng để tạo tài liệu trang hướng dẫn

15. 3. Kiểu dáng và màu sắc trong đầu ra ứng dụng

Việc sử dụng màu và kiểu ANSI không giới hạn ở và

Các ứng dụng có thể sử dụng trực tiếp lớp picocli

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
694 để tạo đầu ra có màu. Bằng cách sử dụng giá trị enum
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
695, picocli sẽ cho biết liệu nó có thể phát ra mã thoát ANSI hay chỉ văn bản thuần túy

Java

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
99

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
00

15. 4. nhiều màu sắc hơn

Hầu hết các thiết bị đầu cuối đều hỗ trợ một

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
01

Các màu từ bảng màu 256 có thể được chỉ định bởi các giá trị chỉ mục của chúng hoặc bởi các thành phần RGB của chúng. Các thành phần RGB phải được phân tách bằng dấu chấm phẩy

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
476 và mỗi thành phần phải nằm trong khoảng từ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
186 đến
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
698, bao gồm cả

Ví dụ:

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
699 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
700

Python chuyển sang ansi

15. 5. Định cấu hình các phần tử cố định

15. 5. 1. Bảng màu

Picocli sử dụng bảng màu mặc định cho các tùy chọn, tham số và lệnh. Không có chú thích nào để sửa đổi bảng màu này, nhưng nó có thể được thay đổi theo chương trình

Đoạn mã dưới đây cho biết cách có thể chỉ định bảng màu tùy chỉnh để định cấu hình kiểu thông báo trợ giúp sử dụng

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
02

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
03

Khi sử dụng API, bạn có thể định cấu hình một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
693 như thế này

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
04

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
05

15. 5. 2. Ghi đè lược đồ màu

Các thuộc tính hệ thống sau sẽ ghi đè các kiểu lược đồ màu. Điều này cho phép người dùng cuối điều chỉnh thiết lập màu thiết bị đầu cuối cá nhân của họ

Thuộc tính hệ thống để ghi đè bảng màu

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
06

Ví dụ

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
07

Giá trị thuộc tính hệ thống có thể chỉ định nhiều kiểu được phân tách bằng dấu phẩy

15. 6. Nền tảng được hỗ trợ

Picocli sẽ chỉ phát mã thoát ANSI trên các nền tảng được hỗ trợ. Để biết chi tiết, xem

15. 6. 1. Unix và Linux

Hầu hết các nền tảng Unix và Linux đều hỗ trợ màu ANSI nguyên bản. Trên Windows, khi picocli phát hiện nó đang chạy dưới một biến thể Unix như Cygwin hoặc MSYS(2) trên Windows, nó sẽ hiển thị các màu và kiểu ANSI, nếu không, nó sẽ không phát ra mã ANSI

15. 6. 2. các cửa sổ

Hiển thị màu sắc trên Windows Command Console và PowerShell yêu cầu thêm một chút công việc

Cách dễ nhất để thực hiện điều này là sử dụng thư viện Jansi trong ứng dụng của bạn

Không có điều nào dưới đây là bắt buộc. Nếu không được hỗ trợ, picocli sẽ không phát ra mã thoát ANSI và mọi thứ sẽ hoạt động mà không có màu

Jansi

Để sử dụng Jansi, bạn cần kích hoạt nó trong ứng dụng của mình. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
08

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
09

Jansi trong hình ảnh bản địa GraalVM

Trong các ứng dụng Java được biên dịch thành hình ảnh gốc GraalVM cho Windows, bản thân Jansi không đủ để hiển thị màu sắc. Điều này một phần là do GraalVM yêu cầu cấu hình và một phần là do Jansi bên trong phụ thuộc vào các thuộc tính hệ thống không chuẩn, không có dự phòng duyên dáng nếu các thuộc tính này không có (như trường hợp của GraalVM)

Người dùng có thể quan tâm đến việc kết hợp Jansi với picocli-jansi-graalvm cho đến khi sự cố này được khắc phục. Ví dụ sử dụng

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
10

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
11

Để tham khảo. Không có Jansi trên Windows 10 Command Console và PowerShell

Khi viết bài này, cách thiết thực để lấy màu trong Command Console và PowerShell là sử dụng Jansi. Dưới đây chỉ là để tham khảo

Bắt đầu từ Windows 10, Bảng điều khiển Windows hỗ trợ chuỗi thoát ANSI, nhưng. Trừ khi phần mềm cụ thể mà bạn đang sử dụng (e. g. java) cho phép xử lý ANSI bằng cách gọi API SetConsoleMode với cờ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
703 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
704) (java không có), bạn sẽ không nhìn thấy màu sắc hoặc xử lý ANSI cho ứng dụng đó

Lưu ý rằng có một cài đặt đăng ký để từ chọn tham gia thành không tham gia. Câu trả lời Stack Overflow này có nhiều chi tiết hơn

Lưu ý rằng picocli hiện không bao gồm việc phát hiện xem liệu hỗ trợ cho Virtual Terminal / chuỗi thoát ANSI có được bật hoặc tắt thông qua

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
705 hoặc thay đổi sổ đăng ký hay không. Vì vậy, chỉ thực hiện những thay đổi này là không đủ để ứng dụng dựa trên picocli hiển thị màu sắc. Các ứng dụng đã bật màu qua
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
705 có thể muốn đặt thuộc tính hệ thống
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
707 thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
708. Các môi trường kích hoạt màu thông qua thay đổi Windows Registry có thể muốn đặt biến môi trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
709

Để tham khảo. Hệ thống con Windows cho Linux (WSL)

Bạn có thể muốn giới thiệu người dùng của mình dùng thử Hệ thống con Windows cho Linux (WSL). Điều này cho phép chúng chạy môi trường GNU/Linux — bao gồm hầu hết các công cụ dòng lệnh, tiện ích và ứng dụng — trực tiếp trên Windows, không sửa đổi, không cần máy ảo. Các ứng dụng dựa trên Picocli sẽ hiển thị màu ANSI trong WSL theo mặc định

Để tham khảo. Phần mềm bên thứ 3

Trong phiên bản Windows trước 10, bảng điều khiển lệnh của Windows không hỗ trợ tô màu đầu ra theo mặc định. Một tùy chọn dành cho người dùng cuối là cài đặt Cmder, ConEmu, ANSICON hoặc Mintty (được sử dụng theo mặc định trong GitBash và Cygwin) để thêm hỗ trợ tô màu cho bảng điều khiển lệnh Windows của họ

15. 7. Buộc Bật/Tắt ANSI

Bạn có thể buộc picocli luôn sử dụng mã ANSI hoặc không bao giờ sử dụng mã ANSI bất kể nền tảng là gì

  • Đặt thuộc tính hệ thống

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    707 thành
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    248 buộc picocli sử dụng mã ANSI; . Người dùng của bạn có thể đề cập đến thuộc tính hệ thống này trong tài liệu dành cho ứng dụng dòng lệnh của bạn.

  • Đặt thuộc tính hệ thống

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    707 thành
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    708 (phân biệt chữ hoa chữ thường) buộc picocli chỉ sử dụng mã ANSI nếu picocli đoán rằng quy trình đang sử dụng bảng điều khiển tương tác.
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    716 hoặc picocli đoán ứng dụng đang chạy trong pty giả thiết bị đầu cuối trên trình giả lập Linux trong Windows. Mặt khác, chúng được sử dụng để xác định xem có xuất mã thoát ANSI hay không

  • Bạn có thể quyết định buộc vô hiệu hóa hoặc buộc bật mã thoát ANSI theo chương trình bằng cách chỉ định

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    717 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    718 khi gọi
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    719. Điều này sẽ ghi đè giá trị của thuộc tính hệ thống
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    707. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
12

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
13

15. 8. Heuristic để kích hoạt ANSI

Dưới đây là trình tự chính xác các bước mà picocli sử dụng để xác định có phát ra mã thoát ANSI hay không

  1. Nếu

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    717 hoặc
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    718 là , thông qua thuộc tính hệ thống
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    707 hoặc theo chương trình, giá trị này được sử dụng

  2. ANSI bị tắt khi biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    724 được xác định (bất kể giá trị của nó)

  3. ANSI được bật khi biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    725 được xác định và có bất kỳ giá trị nào khác với
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    186 (không)

  4. ANSI được bật khi thuộc tính hệ thống

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    727 bắt đầu bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    728 và Bảng điều khiển JAnsi được cài đặt

  5. ANSI bị tắt khi biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    729

  6. ANSI bị tắt khi biến môi trường

  7. ANSI bị tắt khi Picocli đoán luồng đầu ra của chương trình không được kết nối với thiết bị đầu cuối. khi

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    731 trả về
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    295. Kiểm tra này được bỏ qua nếu picocli đoán chương trình đang chạy trong môi trường Windows Cygwin, MSYS hoặc MSYS2. khi thuộc tính hệ thống
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    727 bắt đầu bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    728 và biến môi trường
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    735 chứa
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    736 hoặc bắt đầu bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    737 hoặc biến môi trường
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    738 được xác định

  8. ANSI được bật khi biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    739 được xác định

  9. ANSI được bật khi biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    740

  10. ANSI được bật khi biến môi trường

  11. ANSI được bật khi picocli phát hiện chương trình đang chạy trong HĐH không phải Windows (thuộc tính hệ thống

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    727 không bắt đầu bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    728)

  12. ANSI được bật khi picocli đoán chương trình đang chạy trong môi trường Cygwin, MSYS hoặc MSYS2 (biến môi trường

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    735 chứa
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    736 hoặc bắt đầu bằng
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    737 hoặc biến môi trường
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    738 được xác định)

Mã thoát ANSI không được phát ra nếu không áp dụng điều nào ở trên

16. API trợ giúp sử dụng

Để tùy chỉnh thêm thông báo trợ giúp sử dụng, picocli có API Trợ giúp. Lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
748 cung cấp một số thao tác cấp cao và một tập hợp các thành phần như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
749,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
750,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
751, v.v. , có thể được sử dụng để tạo thông báo trợ giúp tùy chỉnh. Thông tin chi tiết về API trợ giúp nằm ngoài phạm vi của tài liệu này, nhưng các phần sau đây cung cấp một số ý tưởng về những gì có thể

16. 1. Sắp xếp lại các phần

Một điều bạn có thể muốn làm là sắp xếp lại các phần của thông báo sử dụng hoặc thêm các phần tùy chỉnh

Picocli 3. 9 giới thiệu API mới để tạo điều kiện tùy chỉnh thông báo trợ giúp sử dụng.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
752 cho phép các ứng dụng cắm vào các lớp con của
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
748 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
754 cho phép các ứng dụng thêm các phần tùy chỉnh vào thông báo trợ giúp sử dụng hoặc xác định lại các phần hiện có

Thông báo trợ giúp sử dụng không còn được mã hóa cứng nữa mà hiện được xây dựng từ trình kết xuất phần được xác định trong

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
755 (hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
756 cho một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282). Theo mặc định, bản đồ này chứa các trình kết xuất phần được xác định trước

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
14

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
15

Các ứng dụng có thể thêm, xóa hoặc thay thế các phần trong bản đồ này. Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
758 (hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
759 cho một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282) trả về các khóa của phần theo thứ tự mà thông báo trợ giúp sử dụng sẽ hiển thị các phần. Các khóa mặc định là (theo thứ tự)

  1. SECTION_KEY_HEADER_HEADING

  2. SECTION_KEY_HEADER

  3. SECTION_KEY_SYNOPSIS_HEADING

  4. SECTION_KEY_SYNOPSIS

  5. SECTION_KEY_DESCRIPTION_HEADING

  6. SECTION_KEY_DESCRIPTION

  7. SECTION_KEY_PARAMETER_LIST_HEADING

  8. SECTION_KEY_AT_FILE_PARAMETER

  9. SECTION_KEY_PARAMETER_LIST

  10. SECTION_KEY_OPTION_LIST_HEADING

  11. SECTION_KEY_OPTION_LIST

  12. SECTION_KEY_COMMAND_LIST_HEADING

  13. SECTION_KEY_COMMAND_LIST

  14. SECTION_KEY_EXIT_CODE_LIST_HEADING

  15. SECTION_KEY_EXIT_CODE_LIST

  16. SECTION_KEY_FOOTER_HEADING

  17. SECTION_KEY_FOOTER

Thứ tự này có thể được sửa đổi bằng phương pháp thiết lập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
761 (hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
762 cho một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282)

16. 1. 1. Ví dụ về phần trợ giúp tùy chỉnh

Ví dụ dưới đây cho thấy cách thêm phần Biến môi trường tùy chỉnh vào thông báo trợ giúp sử dụng

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
16

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
17

Các ví dụ khác để tùy chỉnh thông báo trợ giúp sử dụng có tại đây

16. 2. Bố cục tùy chỉnh

Picocli cũng hỗ trợ bố cục danh sách tùy chọn độc đáo. Một ví dụ về bố cục độc đáo là ứng dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
764, hiển thị nhiều tùy chọn trên mỗi hàng

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
18

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
19

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
20

Điều này có thể đạt được trong picocli bằng cách phân lớp Trợ giúp. lớp bố trí. Xem lớp CustomLayoutDemo trong các bài kiểm tra picocli để biết cách đạt được điều này

17. tiểu ban

Các công cụ dòng lệnh phức tạp, như công cụ nổi bật

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765, có nhiều lệnh con (e. g. ,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
766,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
767, …), mỗi cái có bộ tùy chọn và tham số vị trí riêng. Picocli giúp dễ dàng có các lệnh với các lệnh phụ và các lệnh phụ ở bất kỳ mức độ chuyên sâu nào

17. 1. Ví dụ về tiểu ban

Nếu bạn muốn tiếp tục và xem một số ví dụ trước, những tài nguyên này có thể hữu ích

  • Phần bên dưới có một ví dụ sử dụng cả cú pháp

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    768 và cú pháp phương thức được chú thích bởi
    
      info.picocli
      picocli
      4.7.0
    
    91

  • Hướng dẫn nhanh có một tiểu ban nổi bật, với lời giải thích chi tiết

  • Mô-đun mã

    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    241 có phần lệnh con với một số mẫu mã tối thiểu, giải thích việc sử dụng các lệnh con thông qua các phương thức và khi được định nghĩa trong lớp riêng của chúng

  • Mô-đun mã

    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    241 cũng hiển thị các ví dụ về lệnh con trong các ngôn ngữ khác, như Scala và Kotlin, cũng như Java

17. 2. Đăng ký tiểu ban theo cách khai báo

Các tiểu ban có thể được đăng ký một cách khai báo với thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
773 của chú thích

  info.picocli
  picocli
  4.7.0
91 vì picocli 0. 9. 8. Đây là cách được đề xuất khi bạn muốn sử dụng picocli để , hoặc

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
21

hấp dẫn

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
22

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
23

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
24

Các lệnh con được tham chiếu trong thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
773 phải có chú thích

  info.picocli
  picocli
  4.7.0
91 với thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
564 hoặc một ngoại lệ được đưa ra từ hàm tạo
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777. Tên này sẽ được sử dụng để tạo trợ giúp sử dụng và để nhận dạng các lệnh con khi phân tích cú pháp dòng lệnh. Tên lệnh phân biệt chữ hoa chữ thường theo mặc định, nhưng đây là

Bộ chuyển đổi loại tùy chỉnh đã đăng ký trên phiên bản

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 sẽ áp dụng cho tất cả các lệnh con đã được khai báo trên lệnh chính với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
773

Các lệnh con được tham chiếu trong thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
773 cần phải có một hàm tạo không đối số công khai để được khởi tạo, trừ khi a được cài đặt để khởi tạo các lớp

Trước picocli 4. 2, các lệnh con được khai báo đã được khởi tạo ngay lập tức khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 cấp cao nhất (đối tượng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
782 trong ví dụ trên) được xây dựng

Từ picocli 4. 2, các lệnh phụ đã khai báo không được khởi tạo cho đến khi chúng được so khớp trên dòng lệnh, trừ khi chúng có trường chú thích


  info.picocli
  picocli
  4.7.0
92 hoặc

  info.picocli
  picocli
  4.7.0
88;

17. 3. Các tiểu ban dưới dạng Phương thức

kể từ picocli 3. 6, có thể đăng ký các lệnh con theo cách rất nhỏ gọn bằng cách có một lớp


  info.picocli
  picocli
  4.7.0
91 với các phương thức được chú thích bởi

  info.picocli
  picocli
  4.7.0
91. Các phương thức tự động của lớp

  info.picocli
  picocli
  4.7.0
91 kèm theo. Xem phần để biết thêm chi tiết và ví dụ

Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 có một ứng dụng ví dụ tối thiểu, thể hiện định nghĩa của các lệnh con thông qua các phương thức. Ví dụ này được mã hóa bằng Java, Kotlin và Scala

17. 4. Đăng ký tiểu ban theo chương trình

Các tiểu ban có thể được đăng ký bằng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
789. Bạn nhập tên của lệnh và đối tượng được chú thích để điền vào các tùy chọn lệnh con. Tên đã chỉ định được trình phân tích cú pháp sử dụng để nhận ra các tiểu ban trong các đối số dòng lệnh

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
25

hấp dẫn

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
26

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
27

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
28

Chúng tôi đặc biệt khuyến nghị rằng các tiểu ban có chú thích


  info.picocli
  picocli
  4.7.0
91 với các thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
564 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261

Từ picocli 3. 1, bản tóm tắt trợ giúp sử dụng của lệnh con không chỉ hiển thị tên lệnh phụ mà còn cả tên lệnh cha. Ví dụ: nếu lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765 có lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
766, trợ giúp sử dụng cho lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
766 sẽ hiển thị
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
796

Lưu ý về bộ chuyển đổi loại tùy chỉnh. bộ chuyển đổi loại tùy chỉnh chỉ được đăng ký với các tiểu ban và các tiểu ban lồng nhau đã được thêm vào trước khi đăng ký loại tùy chỉnh. Để đảm bảo bộ chuyển đổi loại tùy chỉnh có sẵn cho tất cả các tiểu ban, hãy đăng ký bộ chuyển đổi kiểu cuối cùng, sau khi thêm các tiểu ban

17. 5. Tiểu ban thực thi

Cách dễ nhất để thiết kế một ứng dụng với các lệnh phụ là để mỗi lệnh và lệnh phụ là một lớp thực hiện


  info.picocli
  picocli
  4.7.0
97 hoặc

  info.picocli
  picocli
  4.7.0
98 hoặc một phương thức được chú thích bởi

  info.picocli
  picocli
  4.7.0
91

Điều này sẽ cho phép bạn phân tích cú pháp dòng lệnh, xử lý các yêu cầu trợ giúp và yêu cầu thông tin phiên bản, xử lý đầu vào không hợp lệ của người dùng và gọi logic nghiệp vụ của tiểu ban do người dùng chỉ định - tất cả điều đó - trong một dòng mã. phương pháp

Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
29

hấp dẫn

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
30

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
31

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
32

Để kiểm tra ví dụ của chúng tôi trên Linux, chúng tôi đã tạo bí danh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
801 để gọi ứng dụng Java của chúng tôi. Đây cũng có thể là một tập lệnh hoặc một hàm gọi chương trình Java của chúng ta

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
33

Tiếp theo, chúng tôi gọi lệnh cấp cao nhất của mình với một tùy chọn như thế này

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
34

Chúng ta cũng có thể chỉ định một tiểu ban

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
35

Và cuối cùng, chúng ta cũng có thể chỉ định một tiểu ban phụ

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
36

Như bạn có thể thấy, lệnh hoặc lệnh con được chỉ định cuối cùng được thực thi và mã thoát của nó được trả về. Xem thêm để biết chi tiết về cấu hình này

17. 6. Khởi tạo trước khi thực hiện

Đôi khi một ứng dụng cần thực hiện một số hành động trước khi thực thi. Với một lệnh duy nhất, bạn có thể chỉ cần thực hiện việc này khi bắt đầu phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794, nhưng trong một ứng dụng có các lệnh con, bạn không muốn lặp lại mã này trong mọi lệnh con

Một ý tưởng là đặt logic khởi tạo được chia sẻ trong chiến lược thực thi tùy chỉnh. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
37

hấp dẫn

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
38

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
39

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
40

Điều này đảm bảo phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
804 được gọi sau khi dòng lệnh được phân tích cú pháp (vì vậy tất cả các tùy chọn và tham số vị trí được gán) nhưng trước khi lệnh con do người dùng chỉ định được thực thi

Ví dụ ghi nhật ký trong

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 cho thấy cách sử dụng điều này để định cấu hình cấp độ nhật ký Log4j dựa trên tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262, trước khi thực thi

17. 7. info.picocli picocli 4.7.0 88 Chú thích

Trong các ứng dụng dòng lệnh có các lệnh con, các tùy chọn của lệnh cấp cao nhất thường được coi là các tùy chọn "toàn cầu" áp dụng cho tất cả các lệnh con. Trước picocli 2. 2, các lệnh con không có cách nào dễ dàng để truy cập các tùy chọn lệnh cha của chúng trừ khi lệnh cha cung cấp các giá trị này trong một biến toàn cục

Chú thích


  info.picocli
  picocli
  4.7.0
88 được giới thiệu trong picocli 2. 2 giúp các lệnh con dễ dàng truy cập các tùy chọn lệnh cha của chúng. các trường lệnh con được chú thích bằng

  info.picocli
  picocli
  4.7.0
88 được khởi tạo với tham chiếu đến lệnh cha. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
41

hấp dẫn

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
42

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
43

Scala

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
44

Lệnh cấp cao nhất ở trên có tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
810 áp dụng cho các lệnh con của nó. Lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
811 có thể sử dụng chú thích

  info.picocli
  picocli
  4.7.0
88 để tham chiếu đến lệnh chính, vì vậy nó có thể dễ dàng truy cập các tùy chọn lệnh chính

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
45

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
46

17. 8. Bí danh tiểu ban

Các lệnh có thể tùy ý xác định thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
813 để cung cấp các tên thay thế sẽ được trình phân tích cú pháp nhận dạng. Bí danh được hiển thị trong đầu ra trợ giúp mặc định. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
47

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
48

Sẽ dẫn đến đoạn trợ giúp này

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
49

17. 9. Thuộc tính lệnh kế thừa

Picocli 4. 6 bổ sung hỗ trợ kế thừa các thuộc tính


  info.picocli
  picocli
  4.7.0
91 với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
815. Các lệnh có phạm vi này có các thuộc tính

  info.picocli
  picocli
  4.7.0
91 được sao chép sang tất cả các lệnh con (và các lệnh con, ở bất kỳ mức độ chuyên sâu nào)

Khi một lệnh con chỉ định một giá trị rõ ràng trong chú thích


  info.picocli
  picocli
  4.7.0
91 của nó, giá trị này được sử dụng thay cho giá trị kế thừa. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
50

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
51

Lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
818 trong ví dụ trên có
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
815, vì vậy các thuộc tính

  info.picocli
  picocli
  4.7.0
91 của nó được kế thừa bởi lệnh con
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
821

Lệnh con

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
821 xác định
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
614 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261 của riêng nó, vì vậy chúng không được kế thừa từ lệnh mẹ. Thông báo trợ giúp cho tiểu ban trông như thế này

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
52

Lưu ý rằng lệnh con đã kế thừa các tùy chọn trợ giúp tiêu chuẩn hỗn hợp (


  info.picocli
  picocli
  4.7.0
94 và

  info.picocli
  picocli
  4.7.0
95), trợ giúp sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
827, và tiêu đề cuối trang và cuối trang. Nó cũng kế thừa chuỗi phiên bản, được hiển thị khi người dùng gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
828

Khi một lệnh có

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
829, các thuộc tính sau được sao chép vào các lệnh con của nó

  • tất cả các thuộc tính trợ giúp sử dụng. description, descriptionHeading, header, headerHeading, footer, footerHeading, customSynopsis, synopsisHeading, synopsisSubcommandLabel, abbreviateSynopsis, optionListHeading, tham sốListHeading, commandListHeading, exitCodeList, exitCodeListHeading, requiredOptionMarker, showDefaultValues, sortOptions, autoWidth, chiều rộng, showAtFileInUsageHelp, showEndOfOptionsDelimiterInUsageHelp, và ẩn

  • mã thoát. exitCodeOnSuccess, exitCodeOnUsageHelp, exitCodeOnVersionHelp, exitCodeOnInvalidInput, exitCodeOnExecutionException

  • các tùy chọn trợ giúp và phiên bản được trộn lẫn bởi

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    511

  • dấu phân cách giữa tùy chọn và tham số tùy chọn

  • phiên bản

  • phiên bảnNhà cung cấp

  • defaultValueProvider

  • lệnh con Có thể lặp lại

  • cho dù lệnh này là một

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    831

Các thuộc tính không được sao chép bao gồm

  • tên lệnh

  • bí danh lệnh

  • các tùy chọn và tham số (ngoài các tùy chọn trợ giúp và phiên bản được trộn lẫn bởi

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    511)

  • mixin khác ngoài

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    511

  • tiểu ban

  • nhóm đối số

17. 10. Tùy chọn kế thừa

Picocli 4. 3 thêm hỗ trợ cho các tùy chọn "kế thừa". Các tùy chọn được xác định bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
815 được chia sẻ với tất cả các tiểu ban (và các tiểu ban, ở bất kỳ mức độ chuyên sâu nào). Các ứng dụng có thể xác định tùy chọn kế thừa trên lệnh cấp cao nhất, ở một nơi, để cho phép người dùng cuối chỉ định tùy chọn này ở bất kỳ đâu. không chỉ trên lệnh cấp cao nhất mà còn trên bất kỳ lệnh con nào và các lệnh con lồng nhau

Các tùy chọn kế thừa hiện không thể được sử dụng trong. Các ứng dụng muốn sử dụng lại Nhóm đối số trên các tiểu ban cần sử dụng. Xem ví dụ này để chia sẻ Nhóm đối số xác định các tùy chọn chung giữa các tiểu ban

Dưới đây là một ví dụ trong đó tùy chọn kế thừa được sử dụng để định cấu hình ghi nhật ký

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
53

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
54

Người dùng có thể chỉ định tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 trên lệnh cấp cao nhất hoặc trên lệnh phụ và tùy chọn này sẽ có tác dụng tương tự

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
55

Chỉ định tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 trên tiểu ban sẽ có tác dụng tương tự. Ví dụ

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
56

Các lệnh con không cần làm bất cứ điều gì để nhận các tùy chọn kế thừa, nhưng một nhược điểm tiềm ẩn là các lệnh con không nhận được tham chiếu đến các tùy chọn kế thừa

Các lệnh con cần kiểm tra giá trị của một tùy chọn được kế thừa có thể sử dụng tham chiếu đến lệnh gốc của chúng và truy cập tùy chọn được kế thừa thông qua tham chiếu gốc. Ngoài ra, đối với các lệnh con như vậy, các tùy chọn chia sẻ thông qua có thể là một cơ chế phù hợp hơn

17. 11. Phân tích thủ công các tiểu ban

Đối với ví dụ sau, chúng tôi giả sử rằng chúng tôi đã tạo một bí danh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765 để gọi ứng dụng Java của chúng tôi. Đây cũng có thể là một tập lệnh hoặc một hàm gọi chương trình Java của chúng ta

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
57

Tiếp theo, chúng tôi gọi lệnh của mình với một số đối số như thế này

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
58

Trong đó

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765 (thực ra là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
840) là lệnh cấp cao nhất, theo sau là tùy chọn chung và lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
841 với các tùy chọn riêng

Thiết lập trình phân tích cú pháp và phân tích cú pháp dòng lệnh có thể giống như thế này

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
59

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
60

Phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
842 trả về một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
235 có thể được truy vấn để lấy thông tin về lệnh cấp cao nhất (lớp Java được gọi bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765 trong ví dụ này). Phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
845 trả về một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
235 lồng nhau nếu trình phân tích cú pháp tìm thấy một lệnh con. Điều này có thể được truy vấn đệ quy cho đến khi tìm thấy lệnh con lồng nhau cuối cùng và phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
845 trả về
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
61

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
62

Bạn có thể quan tâm đến việc giảm xử lý lỗi và mã soạn sẵn khác trong ứng dụng của mình. Xem thêm phần,

17. 12. Tiểu ban con lồng nhau

Khi đăng ký các lệnh con theo cách khai báo, các lệnh con có thể được lồng vào nhau bằng cách chỉ định thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
773 trên các lớp lệnh con

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
63

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
64

Khi đăng ký các lệnh con theo chương trình, đối tượng được chuyển đến phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
850 có thể là một đối tượng được chú thích hoặc một thể hiện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 với các lệnh con lồng nhau của chính nó. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
65

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
66

Theo mặc định, thông báo trợ giúp sử dụng chỉ hiển thị các tiểu ban của lệnh được chỉ định và không hiển thị các tiểu ban phụ lồng nhau. Điều này có thể được tùy chỉnh bằng cách chỉ định của riêng bạn cho phần danh sách lệnh. Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 có một ví dụ cho biết cách thực hiện việc này

Để nhận trợ giúp về cách sử dụng cho các lệnh con lồng nhau, hãy đặt

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
854 trong tất cả các lệnh trong cấu trúc phân cấp và/hoặc thêm
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
519 vào các lệnh con của tất cả các lệnh. Sau đó, bạn có thể nhận được thông báo trợ giúp cho một tiểu ban (phụ) lồng nhau như thế này

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
67

17. 13. Tiểu ban lặp lại

Từ picocli 4. 2, có thể chỉ định rằng các lệnh con của lệnh có thể được chỉ định nhiều lần bằng cách đánh dấu nó bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
856

17. 13. 1. Thí dụ

Dưới đây là một ví dụ trong đó lệnh cấp cao nhất

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
857 được đánh dấu là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
858. Lệnh này có ba tiểu ban,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
859,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
860 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
861

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
68

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
69

Lệnh ví dụ trên cho phép người dùng chỉ định một hoặc nhiều lệnh con của nó nhiều lần. Ví dụ, đây sẽ là một lời gọi hợp lệ

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
70

Trong lời gọi dòng lệnh ở trên, lệnh cấp cao nhất

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
857 được theo sau bởi lệnh phụ của nó là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
859. Tiếp theo, tiếp theo là hai lần xuất hiện khác của
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
859, tiếp theo là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
860 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
861. Đây là tất cả các lệnh "anh chị em", có chung lệnh gốc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
857. Yêu cầu này hợp lệ vì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
857 được đánh dấu bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
858

17. 13. 2. Đặc điểm kỹ thuật tiểu ban lặp lại

Thông thường,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
870 là
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
247, do đó, đối với mỗi lệnh, chỉ có thể chỉ định một trong các lệnh con của nó, có khả năng theo sau chỉ một lệnh con của lệnh con đó, v.v. Về mặt toán học, một chuỗi các lệnh và lệnh con hợp lệ có thể được biểu diễn bằng một cây gốc có hướng bắt đầu từ lệnh cấp cao nhất. Điều này được minh họa bằng sơ đồ dưới đây

Python chuyển sang ansi

Hình 1. Theo mặc định, các chuỗi lệnh và lệnh con hợp lệ tạo thành một cây gốc có hướng

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
870 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 trên một lệnh, các lệnh con của lệnh này có thể xuất hiện nhiều lần. Ngoài ra, một lệnh con có thể được theo sau bởi lệnh "anh chị em" (một lệnh khác có cùng lệnh cha)

Theo thuật ngữ toán học, khi một lệnh cha có thuộc tính này, các chuỗi hợp lệ bổ sung của các lệnh con của nó tạo thành một biểu đồ con được kết nối đầy đủ (một bản tóm tắt hoàn chỉnh)

Các mũi tên chấm xanh lam và xanh lục trong sơ đồ bên dưới minh họa các trình tự bổ sung được phép khi một lệnh có các lệnh con có thể lặp lại

Python chuyển sang ansi

Hình 2. Nếu một lệnh được đánh dấu để cho phép các lệnh con có thể lặp lại, thì các chuỗi hợp lệ bổ sung của các lệnh con của nó sẽ tạo thành một sơ đồ con được kết nối đầy đủ

Lưu ý rằng không hợp lệ khi chỉ định một lệnh con theo sau bởi lệnh cha của nó

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
71

17. 13. 3. Thực thi tiểu ban lặp lại

Mặc định (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
875) chỉ thực hiện các lệnh con cuối cùng với cùng cha. Ví dụ, nếu

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
72

được gọi, chỉ có hai lệnh con cuối cùng là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
876 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
877 (cả hai đều có lệnh cha là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
878) được thực thi theo mặc định. Bạn có thể sử dụng một chiến lược thực thi khác nếu chiến lược này không đáp ứng nhu cầu của bạn

17. 14. Trợ giúp sử dụng cho các tiểu ban

Sau khi đăng ký các lệnh con, gọi phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
879 sẽ hiển thị thông báo trợ giúp sử dụng bao gồm tất cả các lệnh con đã đăng ký. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
73

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
74

Thông báo trợ giúp sử dụng hiển thị các lệnh theo thứ tự chúng đã được đăng ký

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
75

Mô tả của từng tiểu ban trong danh sách lệnh được lấy từ dòng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
614 đầu tiên của tiểu ban, hoặc dòng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261 đầu tiên nếu nó không có _____2614 được xác định

Thông báo trợ giúp sử dụng ở trên được tạo từ các chú thích trên lớp bên dưới

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
76

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
77

Ví dụ trên sử dụng thuộc tính để trộn và tùy chọn và đăng ký lệnh phụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
334

Sử dụng chú thích


  info.picocli
  picocli
  4.7.0
92 cho các tiểu ban cần hiển thị rõ ràng thông báo trợ giúp sử dụng

Từ picocli 3. 1, bản tóm tắt trợ giúp sử dụng của lệnh con không chỉ hiển thị tên lệnh phụ mà còn cả tên lệnh cha. Ví dụ: nếu lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
765 có lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
766, trợ giúp sử dụng cho lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
766 sẽ hiển thị
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
796

Nếu một tiểu ban muốn hiển thị thông báo trợ giúp sử dụng một cách rõ ràng, thì chú thích


  info.picocli
  picocli
  4.7.0
92 có thể hữu ích.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 được chèn có lệnh gốc được khởi tạo chính xác, do đó, trợ giúp sử dụng có thể hiển thị tên đủ điều kiện của lệnh con

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
78

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
79

Ví dụ: xem tiểu ban ví dụ (

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
279), tạo ra thông báo trợ giúp được hiển thị trong

17. 15. Tiểu ban ẩn

Các lệnh có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
633 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 sẽ không được hiển thị trong thông báo trợ giúp sử dụng của lệnh gốc của chúng

Ví dụ: tiểu ban

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
896 bên dưới được chú thích là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
636

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
80

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
81

Thông báo trợ giúp sử dụng cho

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
898 trông như bên dưới. Lưu ý rằng tiểu ban
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
896 không được hiển thị

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
82

17. 16. Tiểu ban trợ giúp

Các lệnh có thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
831 được đặt thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248 được coi là lệnh trợ giúp. Khi picocli gặp lệnh trợ giúp trên dòng lệnh, các tùy chọn bắt buộc và tham số vị trí bắt buộc của lệnh cha không được xác thực (tương tự như)

Xem để biết thêm chi tiết về cách tạo các tiểu ban trợ giúp

import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable
import kotlin.system.exitProcess

@Command(name = "checksum", mixinStandardHelpOptions = true, version = ["checksum 4.0"],
    description = ["Prints the checksum (SHA-256 by default) of a file to STDOUT."])
class Checksum : Callable<Int> {

    @Parameters(index = "0", description = ["The file whose checksum to calculate."])
    lateinit var file: File

    @Option(names = ["-a", "--algorithm"], description = ["MD5, SHA-1, SHA-256, ..."])
    var algorithm = "SHA-256"

    override fun call(): Int {
        val fileContents = Files.readAllBytes(file.toPath())
        val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
        println(("%0" + digest.size * 2 + "x").format(BigInteger(1, digest)))
        return 0
    }
}

fun main(args: Array<String>) : Unit = exitProcess(CommandLine(Checksum()).execute(*args))
09

17. 17. Tiểu ban bắt buộc

Đối với một số bộ lệnh, việc tự gọi lệnh cấp cao nhất sẽ không hợp lý và người dùng được yêu cầu chỉ định một lệnh con

Từ phiên bản 4. 3, picocli giúp bạn dễ dàng thực hiện việc này. chỉ cần sử dụng để bắt đầu ứng dụng của bạn và biến lệnh cấp cao nhất thành một lớp không triển khai


  info.picocli
  picocli
  4.7.0
97 hoặc

  info.picocli
  picocli
  4.7.0
98

Sau đó, khi người dùng chỉ định lệnh cấp cao nhất, không có lệnh phụ, trong khi lệnh cấp cao nhất này không triển khai


  info.picocli
  picocli
  4.7.0
97 hoặc

  info.picocli
  picocli
  4.7.0
98, picocli sẽ gửi nội bộ một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244 với thông báo lỗi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
908. Điều này sẽ khiến cho được gọi, sẽ hiển thị thông báo này cho người dùng

Trước picocli 4. 3, các ứng dụng có thể chỉ ra rằng các lệnh phụ được yêu cầu bằng cách để lệnh cấp cao nhất ném một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244. Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
84

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
85

Theo mặc định, phần tóm tắt của một lệnh có các tiểu ban sẽ hiển thị một dấu

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
609, cho biết rằng một tiểu ban có thể được chỉ định tùy chọn. Để chỉ ra rằng lệnh con là bắt buộc, hãy sử dụng thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
610 để thay thế chuỗi này bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
611 (không có dấu ngoặc kép
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
612 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
613)

17. 18. Lệnh con và giá trị mặc định

Có một số tình huống trong đó picocli không thể phân tích một tùy chọn có giá trị mặc định giống với tên tiểu ban. Để giải quyết vấn đề này, bạn có thể tự phân tích cú pháp tùy chọn bằng cách sử dụng. Một thực hiện đơn giản được hiển thị dưới đây. Xem vấn đề này để biết thêm chi tiết

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
86

18. tái sử dụng

Bạn có thể thấy mình đang xác định các tùy chọn, tham số hoặc thuộc tính lệnh giống nhau trong nhiều ứng dụng dòng lệnh. Để giảm trùng lặp, picocli hỗ trợ hai cách để sử dụng lại các tùy chọn và thuộc tính đó. phân lớp và mixin

18. 1. phân lớp

Một cách để sử dụng lại các tùy chọn và thuộc tính là mở rộng lớp nơi chúng được định nghĩa. Picocli sẽ đi qua hệ thống phân cấp của lớp để kiểm tra các chú thích, vì vậy các thuộc tính


  info.picocli
  picocli
  4.7.0
89,

  info.picocli
  picocli
  4.7.0
90 và

  info.picocli
  picocli
  4.7.0
91 được khai báo trên một lớp cha có sẵn trong tất cả các lớp con

Dưới đây là một lớp ví dụ,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
920, xác định một số thuộc tính trợ giúp sử dụng và tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262 mà chúng tôi muốn sử dụng lại

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
87

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
88

Bây giờ, bất kỳ lệnh nào được xác định trong một lớp mở rộng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
920 sẽ kế thừa tùy chọn
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262 và tạo thông báo trợ giúp sử dụng theo cùng một kiểu rộng rãi. mã ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
89

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
90

18. 2. hỗn hợp

Picocli 3. 0 giới thiệu khái niệm "mixins". Mixins cho phép sử dụng lại mà không cần phân lớp. chú thích picocli từ bất kỳ lớp nào có thể được thêm vào ("trộn lẫn" với) một lệnh. Picocli's nội bộ sử dụng mixin

Mixin là một lớp riêng biệt với các tùy chọn, tham số vị trí, lệnh con và thuộc tính lệnh mà bạn muốn sử dụng lại trong một hoặc nhiều lệnh khác. Một lệnh nhận các tùy chọn hỗn hợp, tham số vị trí và thuộc tính lệnh này được gọi là "mixee"

Mixin có thể được cài đặt một cách khai báo với trường có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924 (hoặc tham số phương thức có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924 cho các phương thức lệnh). Ngoài ra, mixin có thể được cài đặt theo chương trình bằng cách gọi phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
926 với một thể hiện của lớp mixin

Các ví dụ bên dưới lại sử dụng lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
920 mẫu làm mixin

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
87

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
92

18. 2. 1.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924 Chú thích

Một lệnh được xác định trong một lớp có thể bao gồm một mixin bằng cách chú thích một trường bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924. Tương tự, a có thể bao gồm mixin bằng cách khai báo tham số phương thức có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924. Tất cả các chú thích picocli được tìm thấy trong lớp mixin được thêm vào lệnh ("mixee"). Ví dụ

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
93

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
94

Ngoài việc thêm các tùy chọn, lệnh con và thuộc tính lệnh của đối tượng trộn lẫn vào lệnh, đối tượng trộn lẫn cũng được đưa vào trường được chú thích bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924, làm cho lệnh tham chiếu đối tượng trộn lẫn trở nên đơn giản nếu

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
95

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
96

Mixin được thêm bằng chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924 cũng có thể được truy cập thông qua bản đồ được trả về bởi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
934

Dự án GitHub có một ví dụ hoạt động đầy đủ

18. 2. 2. Ví dụ Mixin. Tiều phu

Đây là một lớp mixin ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
935. Nó có một tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 có thể được chỉ định nhiều lần để tăng mức độ chi tiết. Lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
935 cũng cung cấp một số phương thức như
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
938 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
939 có thể được sử dụng để in thông báo tới luồng lỗi tiêu chuẩn

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
97

Kotlin

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
98

Một ứng dụng có thể sử dụng mixin như thế này

Java

import picocli.CommandLine
import picocli.CommandLine.{Command, Option, Parameters}

import java.io.File
import java.math.BigInteger
import java.nio.file.Files
import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = "checksum", mixinStandardHelpOptions = true, version = Array("checksum 4.0"),
  description = Array("Prints the checksum (SHA-256 by default) of a file to STDOUT."))
class Checksum extends Callable[Int] {

  @Parameters(index = "0", description = Array("The file whose checksum to calculate."))
   private var file: File = null

  @Option(names = Array("-a", "--algorithm"), description = Array("MD5, SHA-1, SHA-256, ..."))
  private var algorithm = "SHA-256"

  def call(): Int = {
    val fileContents = Files.readAllBytes(file.toPath)
    val digest = MessageDigest.getInstance(algorithm).digest(fileContents)
    println(("%0" + digest.size * 2 + "x").format(new BigInteger(1, digest)))
    0
  }
}

object Checksum {
  def main(args: Array[String]): Unit = {
    System.exit(new CommandLine(new Checksum()).execute(args: _*))
  }
}
99

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
600

18. 2. 3. Truy cập Mixee từ Mixin

Đôi khi bạn cần viết một lớp Mixin để truy cập mixee (lệnh nơi nó được trộn vào)

Từ picocli 4. 3, điều này có thể được thực hiện với

Theo mặc định,

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 của lớp kèm theo được đưa vào trường có chú thích

  info.picocli
  picocli
  4.7.0
92, nhưng khi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
943 được chỉ định trong lớp mixin, thì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 của lệnh nhận mixin này ("mixee") được đưa vào. Điều này có thể hữu ích khi một mixin chứa logic chung cho nhiều lệnh. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
601

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
602

18. 2. 4. Truy cập Lệnh gốc từ Mixin

kể từ picocli 4. 2, các trường có thể được sử dụng trong mixin. Điều này đưa một tham chiếu đến lệnh cha của mixee vào mixin. Đây là một ví dụ thực hiện

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
603

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
604

Với điều này, tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 có thể được chỉ định trên lệnh cấp cao nhất cũng như các lệnh con của nó, vì vậy tất cả những điều bên dưới đều là các lệnh gọi hợp lệ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
605

Tất cả các lời gọi này sẽ in một số đầu ra, vì tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 đã được chỉ định

18. 2. 5. Thêm Mixins theo chương trình

Ví dụ dưới đây cho thấy cách một mixin có thể được thêm vào theo chương trình bằng phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
926

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
606

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
607

Có thể truy cập các mixin được thêm theo chương trình thông qua bản đồ được trả về bởi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
934. Tiếp tục từ ví dụ trước

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
608

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
609

18. 3. Phạm vi kế thừa

Cơ chế tái sử dụng thứ ba đang thiết lập

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
815

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
829 được sử dụng trong chú thích

  info.picocli
  picocli
  4.7.0
91, tất cả các thuộc tính của

  info.picocli
  picocli
  4.7.0
91, ngoại trừ tên lệnh và các lệnh phụ của nó, sẽ được sao chép vào các lệnh phụ. Xem để biết chi tiết

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
829 được sử dụng trong chú thích

  info.picocli
  picocli
  4.7.0
89, tùy chọn đó được sao chép vào các tiểu ban. Xem để biết chi tiết

18. 4. Trường hợp sử dụng. Định cấu hình Cấp nhật ký với Tùy chọn toàn cầu

Phần này hiển thị một ví dụ chi tiết minh họa Mixins

Đối với các mixin cần được sử dụng lại qua nhiều hơn hai cấp trong hệ thống phân cấp lệnh, việc khai báo một trường sẽ cấp cho mixin quyền truy cập vào hệ thống phân cấp lệnh đầy đủ

Giá trị

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
943 chỉ ra rằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 để tiêm không phải là thông số kỹ thuật của lớp kèm theo, mà là thông số kỹ thuật của lệnh nơi sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924

Ví dụ dưới đây cho thấy một lớp sử dụng Log4j để ghi nhật ký và có tùy chọn "toàn cầu"

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
262 có thể được chỉ định trên bất kỳ lệnh nào để cho phép người dùng định cấu hình cấp độ nhật ký Log4j. Trường có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
956 cho phép mixin leo lên cấu trúc phân cấp lệnh và lưu trữ giá trị "độ chi tiết" ở một nơi duy nhất. Khi ứng dụng được khởi động, chiến lược thực thi tùy chỉnh được sử dụng để định cấu hình cấp độ nhật ký từ giá trị đơn lẻ đó

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
610

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
611

Với điều này, tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
835 có thể được chỉ định trên lệnh cấp cao nhất cũng như các lệnh con, vì vậy tất cả những điều bên dưới đều là các lệnh gọi hợp lệ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
612

Xem thêm ví dụ ghi nhật ký trong

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 để biết cách chỉ sửa đổi cấp độ nhật ký của Log4j ConsoleAppenders từ mức độ chi tiết được chỉ định

18. 5. Tùy chọn chia sẻ trong các tiểu ban

Trường hợp sử dụng trên cũng có thể được thực hiện (với ít mã hơn) thông qua. Các tùy chọn kế thừa là một cơ chế tái sử dụng khác trong đó một ứng dụng xác định một tùy chọn trên một lệnh, sau đó người dùng cuối có thể chỉ định tùy chọn này trên bất kỳ lệnh con nào và nó sẽ đặt giá trị của trường được chú thích trên lệnh cha

18. 6. Sử dụng lại kết hợp

Các cơ chế trên có thể được kết hợp theo bất kỳ cách nào. Các mixin có thể được lồng vào nhau và không có giới hạn về mức độ sâu của các mixin có thể được lồng vào nhau. Một mixin cũng có thể kế thừa các tùy chọn, tham số vị trí và thuộc tính lệnh từ một siêu lớp

Một tùy chọn có cùng tên không nên được xác định nhiều lần hoặc một

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
300 bị ném trong quá trình khởi tạo. Các tham số vị trí cho cùng một vị trí có thể được xác định nhiều lần, chúng có thể cùng tồn tại

Thuộc tính lệnh có thể được xác định nhiều lần, nhưng chỉ có một giá trị được giữ nguyên. Trong trường hợp một thuộc tính lệnh được xác định nhiều lần, định nghĩa trước đó trong danh sách sau sẽ được ưu tiên hơn trong danh sách sau

  1. @Command thuộc tính của chính lệnh

  2. Các thuộc tính trên các lệnh @Mixin

  3. Các thuộc tính trên @Mixin được lồng trong @Mixin của lệnh

  4. Các thuộc tính trên siêu lớp của @Mixin lồng nhau

  5. Các thuộc tính trên siêu lớp của @Mixin

  6. Các thuộc tính trên lớp cha của lệnh

  7. Các thuộc tính trên mixin được thêm vào theo chương trình

19. quốc tế hóa

Kể từ phiên bản 3. 6, phần thông báo trợ giúp sử dụng và mô tả cho các tùy chọn và tham số vị trí có thể được chỉ định trong gói tài nguyên. Gói tài nguyên có thể được đặt thông qua chú thích và lập trình

Để giúp bạn bắt đầu nhanh chóng, mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 có một ví dụ i18n tối thiểu, được mã hóa bằng cả Java và Kotlin

19. 1. Cấu hình

Ví dụ về chú thích

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
613

Ví dụ lập trình

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
614

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
615

19. 2. Gói tài nguyên ví dụ

Gói tài nguyên thuộc tính ví dụ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
616

Đối với các tùy chọn và tham số vị trí, thuộc tính chú thích tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 có thể được sử dụng để bản địa hóa mô tả. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
617

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
618

Mục phù hợp trong gói tài nguyên có thể trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
619

Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 bị bỏ qua, ví dụ: bất kỳ tên tùy chọn nào không có dấu gạch ngang ở đầu

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
620

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
621

Mục phù hợp trong gói tài nguyên có thể trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
622

Ví dụ, đối với khóa dự phòng là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
968

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
623

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
624

Mục phù hợp trong gói tài nguyên có thể trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
625

Đối với các nhóm đối số, hãy sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
099 để chỉ định khóa gói tài nguyên. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
626

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
627

Mục phù hợp trong gói tài nguyên có thể trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
628

Lưu ý rằng văn bản tiêu đề phải kết thúc bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
102 hoặc tùy chọn đầu tiên trong nhóm sẽ nằm trên cùng một dòng. Điều này là phù hợp với khác trong trợ giúp sử dụng

19. 3. Gói tài nguyên được chia sẻ

Tài nguyên cho nhiều lệnh có thể được chỉ định trong một ResourceBundle duy nhất. Các phím và giá trị của chúng có thể được chia sẻ bởi nhiều lệnh (vì vậy bạn không cần phải lặp lại chúng cho mọi lệnh), nhưng cách khác, các phím có thể được thêm tiền tố bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
971 để chỉ định các giá trị khác nhau cho các lệnh khác nhau. Chiến thắng chính cụ thể nhất. Ví dụ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
629

19. 4. Bản địa hóa Trợ giúp Tích hợp

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
519 tích hợp có thể được bản địa hóa như sau

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    973 kiểm soát phần tóm tắt lệnh trợ giúp trong danh sách lệnh con

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    974 là khóa gói tài nguyên cho tùy chọn
    
      info.picocli
      picocli
      4.7.0
    
    94 của tiểu ban trợ giúp

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    976 là khóa gói tài nguyên cho tham số vị trí
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    611 của tiểu ban trợ giúp

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
630

19. 5. Bản địa hóa các giá trị mặc định

Các tùy chọn có a có thể sử dụng biến

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260 trong phần mô tả tùy chọn được bản địa hóa trong gói tài nguyên

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
631

19. 6. Kiểm soát địa phương

Trong ứng dụng được bản địa hóa của bạn, bạn có thể chỉ định ngôn ngữ để xác định ngôn ngữ của văn bản tin nhắn và đầu ra trợ giúp. Một cách để kiểm soát ngôn ngữ là cung cấp cho đối số VM

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
979 khi chạy ứng dụng. Một cách tiếp cận dễ tiếp cận và thân thiện với người dùng hơn là triển khai tham số dòng lệnh (e. g.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
980) bên trong ứng dụng của bạn có thể được sử dụng để thay đổi ngôn ngữ. Kỹ thuật thứ hai yêu cầu cách tiếp cận hai giai đoạn để phân tích cú pháp trong ứng dụng của bạn để có được thứ tự tải hợp lệ. Ví dụ tối thiểu dưới đây minh họa cách thực hiện phương pháp tiếp cận hai giai đoạn này

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
632

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
633

Bây giờ hãy đặt tệp thuộc tính mặc định (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
981, bằng tiếng Anh) và biến thể ngôn ngữ tiếng Tây Ban Nha (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
982) vào vị trí

gói của tôi. của cải

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
634

mybundle_es. của cải

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
635

Cuối cùng, chúng tôi đã sẵn sàng để chạy ứng dụng của mình

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
636

Không có tham số dòng lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
980 được cung cấp, các văn bản thông báo được in bằng ngôn ngữ mặc định (ở đây. Tiếng Anh)

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
637

Để kiểm soát ngôn ngữ được chọn cho đầu ra của chúng tôi, chúng tôi phải sử dụng tham số dòng lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
980

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
638

Bây giờ văn bản tin nhắn của chúng tôi được in bằng tiếng Tây Ban Nha

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
639

Sử dụng tham số dòng lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
980, người ta cũng có thể xác định ngôn ngữ của đầu ra trợ giúp của ứng dụng của bạn. Mô-đun
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
241 có các ví dụ với đầu ra trợ giúp được bản địa hóa, được triển khai đầy đủ, được mã hóa bằng cả Java và Kotlin

20. Nội suy biến

Kể từ phiên bản 4. 0, picocli hỗ trợ nội suy biến (mở rộng biến) trong thuộc tính chú thích cũng như thuộc tính văn bản của API lập trình

20. 1. Ví dụ nội suy biến

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
640

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
641

Trong Kotlin, ký hiệu đô la ($) phải được thoát bằng dấu gạch chéo ngược (\) để ngăn nội suy chuỗi Kotlin xảy ra.

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
987

20. 2. Biến được xác định trước

Các biến sau được xác định trước

BiếnS inceSử dụng trong ý nghĩa

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260

3. 2

mô tả cho một tùy chọn hoặc tham số vị trí

được thay thế bằng tùy chọn đó hoặc tham số vị trí

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
287

4. 0

mô tả cho một tùy chọn với tham số tùy chọn

được thay thế bằng tùy chọn đó hoặc tham số vị trí

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
228

4. 6

mô tả cho tùy chọn bản đồ hoặc tham số vị trí cho phép tham số chỉ có khóa

được thay thế bằng tùy chọn đó hoặc tham số vị trí

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
662

3. 2

mô tả cho một tùy chọn hoặc tham số vị trí

được thay thế bằng các ứng cử viên hoàn thành cho tùy chọn đó hoặc tham số vị trí

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
268

4. 0

bất kỳ phần nào của thông báo trợ giúp sử dụng cho một lệnh

thay thế bằng tên của lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
993

4. 0

bất kỳ phần nào của thông báo trợ giúp sử dụng cho một lệnh

được thay thế bằng tên đủ điều kiện của lệnh (nghĩa là trước tên cha đủ điều kiện của nó)

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
994

4. 0

bất kỳ phần nào của thông báo trợ giúp sử dụng cho một lệnh

được thay thế bằng tên của lệnh cha của nó

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
995

4. 0

bất kỳ phần nào của thông báo trợ giúp sử dụng cho một lệnh

được thay thế bằng tên đủ điều kiện của lệnh cha của nó (nghĩa là trước tên của (các) lệnh tổ tiên của lệnh cha)

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
996

4. 4

bất kỳ phần nào của thông báo trợ giúp sử dụng cho một lệnh

được thay thế bằng tên của lệnh cấp cao nhất (trong bộ lệnh có các lệnh con)

20. 3. Biến tùy chỉnh

Ngoài ra, bạn có thể xác định các biến của riêng mình. Hiện tại các cú pháp sau được hỗ trợ

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    997. tra cứu thuộc tính hệ thống, được thay thế bằng giá trị của
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    998

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    999. tra cứu biến môi trường, được thay thế bằng giá trị của
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    000

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    001. tra cứu giá trị của
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    002 trong gói tài nguyên của lệnh

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    003. tìm kiếm tất cả các thuộc tính hệ thống ở trên, đầu tiên, sau đó là các biến môi trường và cuối cùng là gói tài nguyên của lệnh

20. 4. Giá trị mặc định cho biến tùy chỉnh

Bạn có thể chỉ định giá trị mặc định để sử dụng khi không tìm thấy giá trị nào cho biến tùy chỉnh. Cú pháp để chỉ định giá trị mặc định là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
004, trong đó
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
437 là tên biến và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
438 là giá trị mặc định sẽ sử dụng nếu không tìm thấy
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
437

Vì vậy, đối với các tra cứu riêng lẻ, điều này trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
642

Giá trị mặc định có thể chứa các biến tùy chỉnh khác. Ví dụ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
643

Biến trên được mở rộng như sau. Trước tiên, hãy thử tìm khóa

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
437 trong gói tài nguyên của lệnh. Nếu không tìm thấy
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
437 trong gói tài nguyên, hãy lấy giá trị của biến môi trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
438. Nếu không tồn tại biến môi trường
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
438, hãy lấy giá trị của thuộc tính hệ thống
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
012. Cuối cùng, nếu không tồn tại thuộc tính hệ thống
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
012, giá trị của biểu thức sẽ trở thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
014

20. 5. Biến thoát

Đôi khi bạn muốn hiển thị một chuỗi như

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
015 trong phần mô tả. Một ký tự ________ 3016 có thể được thoát bằng một ký tự ________ 3016 khác. Do đó, biến
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
018 sẽ không được hiểu là biến
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
019 mà thay vào đó sẽ được hiểu là biến
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
020

20. 6. Tắt nội suy biến

Nội suy biến có thể được tắt cho hệ thống phân cấp lệnh đầy đủ bằng cách gọi

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
021 hoặc cho một lệnh cụ thể bằng cách gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
022

20. 7. Hạn chế của nội suy biến

Một số giá trị thuộc tính cần được giải quyết sớm, khi mô hình được xây dựng từ các giá trị chú thích

Đặc biệt

  • tên lệnh và bí danh, tên tùy chọn, tên mixin

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    007 (đối với các tùy chọn và tham số vị trí)

  • import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    268 (đối với tham số vị trí)

  • @Grab('info.picocli:picocli-groovy:4.7.0')
    import picocli.CommandLine
    import static picocli.CommandLine.*
    
    import java.security.MessageDigest
    import java.util.concurrent.Callable
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    class Checksum implements Callable<Integer> {
    
        @Parameters(index = '0', description = 'The file whose checksum to calculate.')
        File file
    
        @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
        String algorithm = 'SHA-256'
    
        Integer call() throws Exception {
            println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
            0
        }
    
        static void main(String[] args) {
            System.exit(new CommandLine(new Checksum()).execute(args))
        }
    }
    631 (đối với lệnh)

Các thuộc tính này có thể chứa các biến, nhưng hãy lưu ý những hạn chế

Nếu các thuộc tính này có biến và các biến có giá trị khác sau khi mô hình được xây dựng, thay đổi sẽ không được phản ánh trong mô hình

21. Mẹo & Thủ thuật

21. 1. API có lập trình

Picocli cũng cung cấp API có lập trình, ngoài API chú thích

API có lập trình cho phép các ứng dụng tạo động các tùy chọn dòng lệnh một cách nhanh chóng, trong đó không phải tất cả các tùy chọn đều được biết trước. Nó cũng cho phép sử dụng picocli trong các ngôn ngữ JVM khác không hỗ trợ chú thích

Một trường hợp sử dụng khác là tạo các ngôn ngữ dành riêng cho miền thành ngữ để xử lý các đối số dòng lệnh. Ví dụ: Groovy's được triển khai bằng API lập trình của picocli

21. 2. Phương pháp info.picocli picocli 4.7.0 89 và info.picocli picocli 4.7.0 90

Kể từ phiên bản 3. 2, Chú thích


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 có thể được thêm vào các phương thức cũng như các trường của một lớp

Đối với các lớp cụ thể, hãy chú thích các phương thức "setter" (các phương thức chấp nhận một tham số) và khi tùy chọn được chỉ định trên dòng lệnh, picocli sẽ gọi phương thức có giá trị được chỉ định trên dòng lệnh, được chuyển đổi thành loại tham số của phương thức

Ngoài ra, bạn có thể chú thích các phương thức "giống như getter" (các phương thức trả về một giá trị) trên một giao diện và picocli sẽ tạo một phiên bản của giao diện trả về các giá trị được chỉ định trên dòng lệnh, được chuyển đổi thành kiểu trả về của phương thức

21. 2. 1. Các phương thức chú thích của một giao diện

Chú thích


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 có thể được sử dụng trên các phương thức của giao diện trả về giá trị. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
644

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
645

Bạn sử dụng nó bằng cách chỉ định lớp của giao diện

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
646

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
647

21. 2. 2. Các phương thức chú thích của một lớp cụ thể

Các chú thích


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 có thể được sử dụng trên các phương thức của một lớp chấp nhận một tham số. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
648

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
649

Bạn sử dụng nó bằng cách chuyển một thể hiện của lớp

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
650

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
651

Các phương thức được chú thích bằng


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90 có thể thực hiện xác thực đầu vào đơn giản bằng cách ném một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244 khi các giá trị không hợp lệ được chỉ định trên dòng lệnh

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
652

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
653

Các ứng dụng sử dụng lại một phiên bản

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 duy nhất cho nhiều lệnh gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 cần lưu ý những điều sau

Các tùy chọn phương thức setter được chú thích đặt rõ ràng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
040 sẽ nhận được một cuộc gọi với tham số
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
295 khi dòng lệnh không bao gồm tùy chọn đó

Nếu chú thích phương thức setter không đặt rõ ràng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
040, thì phương thức này sẽ không được gọi khi dòng lệnh không bao gồm tùy chọn đó. Điều này có nghĩa là giá trị sẽ giữ nguyên giá trị của lệnh gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154 trước đó, điều này có thể không được mong muốn. (Điều này chỉ xảy ra đối với các ứng dụng sử dụng lại một phiên bản
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 duy nhất cho nhiều lệnh gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
204 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
154. )

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
654

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
655

21. 3. Phương pháp info.picocli picocli 4.7.0 91

kể từ picocli 3. 6, các phương thức có thể được chú thích bằng


  info.picocli
  picocli
  4.7.0
91. Các tham số phương thức cung cấp các tùy chọn lệnh và tham số. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
656

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
657

Trợ giúp sử dụng của lệnh trên trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
658

Xem bên dưới để biết ví dụ sử dụng a để xác định mô tả trợ giúp sử dụng bên ngoài mã

Đối với các tham số vị trí, chú thích


  info.picocli
  picocli
  4.7.0
90 có thể được bỏ qua trên các tham số phương thức

Nếu được biên dịch với cờ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
051 trên Java 8 trở lên, thì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
570 của tham số vị trí được lấy từ tên tham số phương thức bằng cách sử dụng phản chiếu thay vì arg0, arg1 chung, v.v.

21. 3. 1. Phương pháp tiểu ban

Nếu lớp kèm theo được chú thích bằng


  info.picocli
  picocli
  4.7.0
91, các lệnh phương thức sẽ tự động được thêm dưới dạng các lệnh phụ vào lệnh lớp, trừ khi lệnh lớp có thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
054. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
659

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
660

Sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
054 trên chú thích của lớp

  info.picocli
  picocli
  4.7.0
91 nếu các phương thức được chú thích bởi

  info.picocli
  picocli
  4.7.0
91 trong lớp này không nên được thêm dưới dạng các lệnh con

21. 3. 2. Văn bản mô tả trong ResourceBundle

Trợ giúp sử dụng của lệnh ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
279 ở trên là rất nhỏ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
661

Bạn có thể sử dụng a để di chuyển các mô tả ra khỏi mã

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
662

Với gói tài nguyên này, trợ giúp sử dụng cho lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
279 trông như thế này

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
663

21. 3. 3. Hỗ trợ Mixin trong Phương pháp

  info.picocli
  picocli
  4.7.0
91

kể từ picocli 3. 8, phương thức


  info.picocli
  picocli
  4.7.0
91 chấp nhận tham số
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
924. Tất cả các tùy chọn và tham số vị trí được xác định trong lớp mixin được thêm vào lệnh

Thí dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
664

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
665

Trong ví dụ trên, các tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
020 được thêm vào tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
372 của lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
066

21. 4. info.picocli picocli 4.7.0 92 Chú thích

Picocli 3. 2 giới thiệu chú thích


  info.picocli
  picocli
  4.7.0
92 để đưa mô hình lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 vào trường lệnh

Từ picocli 4. 6, Các yếu tố chú thích


  info.picocli
  picocli
  4.7.0
92 có thể được sử dụng trong

Điều này hữu ích khi một lệnh cần sử dụng API picocli, chẳng hạn như để đi qua hệ thống phân cấp lệnh và lặp lại các lệnh anh chị em của nó. Điều này bổ sung cho chú thích


  info.picocli
  picocli
  4.7.0
88;

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
666

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
667

21. 4. 1.
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
956 Chú thích

Kể từ picocli 4. 3, chú thích


  info.picocli
  picocli
  4.7.0
92 có phần tử
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
075. Giá trị là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
076 theo mặc định, có nghĩa là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 của lớp kèm theo được đưa vào trường có chú thích

  info.picocli
  picocli
  4.7.0
92

Đối với các lớp được sử dụng làm a , có một giá trị khác có thể hữu ích. Khi

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
940 được chỉ định trong một lớp mixin, thì
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282 của lệnh nhận mixin này ("mixee") được đưa vào trường có chú thích

  info.picocli
  picocli
  4.7.0
92. Điều này có thể hữu ích khi một mixin chứa logic chung cho nhiều lệnh. Xem để biết thêm chi tiết

21. 5. Nhà máy tùy chỉnh

Đã đăng ký khai báo và phải được khởi tạo bằng cách nào đó. kể từ picocli 2. 2, một nhà máy tùy chỉnh có thể được chỉ định khi xây dựng phiên bản

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777. Điều này cho phép toàn quyền kiểm soát việc tạo đối tượng và mở ra các khả năng cho Inversion of Control và Dependency Injection (xem phần ). Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
668

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
669

Các nhà máy tùy chỉnh cần triển khai giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
083

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
670

Nếu không có nhà máy nào được chỉ định, nhà máy mặc định sẽ được sử dụng. Nhà máy mặc định yêu cầu các lớp khởi tạo phải có hàm tạo không có đối số công khai. nó khởi tạo lớp bằng cách gọi đầu tiên là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
084 và nếu không thành công, hãy gọi
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
085

Từ phiên bản 4. 0, picocli ủy quyền tất cả việc tạo đối tượng cho nhà máy, bao gồm tạo các phiên bản

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201 để nắm bắt các giá trị

  info.picocli
  picocli
  4.7.0
89. Trước đây, các đối tượng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
201 được khởi tạo riêng mà không liên quan đến nhà máy

Các nhà máy tùy chỉnh nên quay trở lại nhà máy mặc định. Một cái gì đó như thế này

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
671

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
672

21. 6. Chuyển đổi mô hình

Từ picocli 4. 6, có thể sử dụng API chú thích để sửa đổi mô hình (lệnh, tùy chọn, tiểu ban, v.v. ) động trong thời gian chạy. Chú thích


  info.picocli
  picocli
  4.7.0
91 hiện có thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
090 nơi các ứng dụng có thể chỉ định một lớp triển khai giao diện
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
091

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
673

Điều này cho phép các ứng dụng tự động thêm hoặc xóa các tùy chọn, tham số vị trí hoặc lệnh con hoặc sửa đổi lệnh theo bất kỳ cách nào khác, dựa trên một số điều kiện thời gian chạy

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
674

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
675

Tất cả các biến áp được gọi một lần, sau khi hệ thống phân cấp lệnh đầy đủ được xây dựng và trước khi bất kỳ đối số dòng lệnh nào được phân tích cú pháp

Nếu máy biến áp mô hình của bạn được khai báo là lớp lồng nhau, hãy đảm bảo bạn đánh dấu lớp này là

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
781, nếu không picocli sẽ không thể khởi tạo lớp máy biến áp của bạn

21. 7. Chỉ mục tham số tự động

21. 7. 1. Chỉ mục tự động

có thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 xác định (các) đối số dòng lệnh nào được ghi lại. Có thể bỏ thuộc tính
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 và để picocli tự động gán chỉ mục

Điều này có nghĩa là những thứ khác nhau đối với tham số vị trí đơn giá trị và đa giá trị

Đối với các tham số vị trí đa giá trị (mảng hoặc tập hợp), việc bỏ qua thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 có nghĩa là trường nắm bắt tất cả các tham số vị trí (tương đương với
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
274)

Đối với các tham số vị trí một giá trị, hành vi của picocli đã thay đổi kể từ phiên bản 4. 3. trước picocli 4. 3, chỉ mục mặc định cho tham số vị trí một giá trị cũng là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
274, mặc dù chỉ có thể ghi lại một giá trị (thường là đối số đầu tiên). Từ phiên bản 4. 3, chỉ mục mặc định là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
098, yêu cầu picocli tự động gán chỉ mục, bắt đầu từ 0, dựa trên các tham số vị trí khác được xác định trong cùng một lệnh. Một ví dụ đơn giản có thể trông như thế này

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
676

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
677

Picocli khởi tạo các trường có giá trị tại chỉ mục đã chỉ định trong mảng đối số

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
678

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
679

Các chỉ mục tự động phụ thuộc vào khả năng phản ánh Java và bộ xử lý chú thích Java để lặp qua các trường theo thứ tự khai báo trong mã nguồn. Chính thức điều này không được đảm bảo bởi thông số Java. Trong thực tế, điều này đã hoạt động trong các JVM của Oracle và OpenJDK từ Java 6, nhưng có một số rủi ro là điều này có thể không hoạt động trong tương lai hoặc trên các JVM khác. Nói chung, đối với các tham số vị trí một giá trị, sử dụng là tùy chọn an toàn hơn. (Các tham số vị trí đa giá trị có thể bỏ qua thuộc tính

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
268 một cách an toàn. )

Các phương thức không thể được lặp đi lặp lại theo thứ tự có thể dự đoán được. Đối với các ứng dụng có hoặc kết hợp các phương thức có chú thích


  info.picocli
  picocli
  4.7.0
90 và các trường có chú thích

  info.picocli
  picocli
  4.7.0
90, chúng tôi khuyên bạn nên sử dụng cho các tham số vị trí một giá trị

21. 7. 2. Chỉ mục tự động được neo

Chỉ mục tự động mặc định (

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
098) cho các tham số vị trí một giá trị là "được neo ở mức 0". nó bắt đầu từ 0 và được tăng lên với mỗi tham số vị trí bổ sung

Đôi khi bạn muốn có các chỉ mục được gán tự động từ một điểm bắt đầu khác 0. Điều này có thể hữu ích khi xác định với các tham số vị trí

Để thực hiện điều này, hãy chỉ định một chỉ mục có điểm neo và ký tự

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
477 để chỉ ra rằng picocli sẽ bắt đầu tự động gán chỉ mục từ điểm neo đó. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
680

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
681

21. 7. 3. Kết hợp các chỉ mục rõ ràng và tự động

Nếu một lệnh xác định tham số vị trí có chỉ mục rõ ràng tại điểm neo, thì chỉ mục tự động được neo tại điểm đó sẽ bắt đầu từ chỉ mục rõ ràng cộng với 1. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
682

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
683

21. 7. 4. Chỉ mục tự động chưa được neo

Đôi khi bạn muốn có các chỉ mục được gán tự động ở cuối. Điều này có thể hữu ích khi xác định với các tham số vị trí

Để thực hiện điều này, hãy chỉ định một chỉ mục có ký tự

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
477 để cho biết rằng picocli sẽ tự động chỉ định các chỉ mục ở cuối. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
684

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
685

21. 7. 5. Không kết hợp các chỉ mục không được neo với các chỉ mục kết thúc mở

Không thể kết hợp các chỉ mục chưa được neo (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
477) với các chỉ mục rõ ràng có kết thúc mở (
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
039). tham số với chỉ mục kết thúc mở sẽ nắm bắt tất cả các đối số và không có vị trí "sau các chỉ mục khác" có thể được chỉ định

Ví dụ dưới đây sẽ luôn báo lỗi

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
108

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
686

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
687

21. 8. Cải thiện hỗ trợ cho tiếng Trung, tiếng Nhật và tiếng Hàn

Picocli sẽ căn chỉnh thông báo trợ giúp sử dụng để vừa với một số chiều rộng do người dùng xác định (80 cột theo mặc định). Một số ký tự trong tiếng Trung, tiếng Nhật và tiếng Hàn (CJK) rộng hơn các ký tự khác. Nếu các ký tự đó được coi là có cùng chiều rộng với các ký tự khác, thông báo trợ giúp sử dụng có thể vượt ra ngoài lề phải

từ 4. 0, picocli sẽ sử dụng 2 cột cho các ký tự rộng này khi tính toán vị trí đặt dấu ngắt dòng, giúp văn bản thông báo trợ giúp sử dụng tốt hơn

Điều này có thể được tắt bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
109

21. 9. Tùy chọn Boolean với Tham số

Theo mặc định, giá trị của trường boolean được đặt thành logic âm của giá trị mặc định khi tùy chọn được chỉ định trên dòng lệnh

Có thể cho phép người dùng cuối chỉ định rõ ràng "true" hoặc "false" làm tham số cho tùy chọn boolean bằng cách xác định thuộc tính rõ ràng. Tùy chọn boolean với

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
232 chấp nhận tham số từ 0 đến 1,
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
018 có nghĩa là tùy chọn phải có một tham số. Ví dụ

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
688

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
689

Các cách gọi chương trình sau đây sẽ được chấp nhận (các giá trị không phân biệt chữ hoa chữ thường)

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
690

Nhưng cố gắng chỉ định tùy chọn

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
249 mà không có tham số hoặc có giá trị khác "true" hoặc "false" (không phân biệt chữ hoa chữ thường) sẽ dẫn đến kết quả là
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
244

21. 10. Giá trị thập lục phân

Các giá trị số được hiểu là số thập phân theo mặc định. Nếu bạn muốn picocli linh hoạt hơn, bạn có thể đăng ký trình chuyển đổi loại tùy chỉnh ủy quyền cho phương thức chuyển đổi chuỗi thành số

Phương pháp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
114 xem xét tiền tố để xác định cơ số, vì vậy các số bắt đầu bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
115,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
116 hoặc
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
293 được hiểu là số thập lục phân, các số bắt đầu bằng
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
186 được hiểu là số bát phân và nếu không thì số đó được hiểu là số thập phân

java8

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
691

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
692

java5

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
693

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
694

21. 11. Dấu tách tùy chọn-tham số

21. 11. 1. Dấu phân cách mặc định

Các tùy chọn có thể lấy một tham số tùy chọn (còn được gọi là đối số tùy chọn). Đối với các tùy chọn ngắn kiểu POSIX (như

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
013 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
064), tham số tùy chọn có thể được đính kèm với tùy chọn hoặc có thể được phân tách bằng dấu cách hoặc chuỗi phân tách (
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220 theo mặc định). Đó là, tất cả những điều dưới đây là tương đương

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
695

Các tên tùy chọn dài (như

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
122) phải được phân tách khỏi tham số tùy chọn của chúng bằng dấu cách hoặc chuỗi phân tách (
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
220 theo mặc định). Tức là, hai ví dụ đầu tiên bên dưới hợp lệ nhưng ví dụ cuối cùng không hợp lệ

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
696

21. 11. 2. Dấu phân cách tùy chỉnh

Chuỗi phân cách có thể được tùy chỉnh theo chương trình hoặc khai báo

Sử dụng thuộc tính

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
631 của chú thích

  info.picocli
  picocli
  4.7.0
91 để khai báo đặt chuỗi dấu phân cách

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
697

Kotlin

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
698

Java

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
699

Kotlin


  info.picocli
  picocli
  4.7.0
00

Ngoài ra, chuỗi dấu phân cách có thể được thay đổi theo chương trình bằng phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
126. Ví dụ

Java


  info.picocli
  picocli
  4.7.0
01

Kotlin


  info.picocli
  picocli
  4.7.0
02

21. 12. Thực tiễn tốt nhất cho giao diện dòng lệnh

Picocli giúp bạn dễ dàng theo dõi bộ ảnh mới và hiện đại này

Hơi cũ, nhưng cổ điển. khi thiết kế ứng dụng dòng lệnh của bạn, for giao diện dòng lệnh và có thể hữu ích

Nói chung, nhiều ứng dụng sử dụng tùy chọn cho các giá trị tùy chọn và tham số cho các giá trị bắt buộc. Tuy nhiên, picocli cho phép bạn thực hiện các tùy chọn cần thiết nếu muốn, xem

21. 13. Khối văn bản cho Java 15

Khi viết chương trình dòng lệnh, bạn có thể sử dụng tính năng "Khối văn bản" mới của Java 15. Khối văn bản nhiều dòng có thể được sử dụng trong mô tả lệnh và tùy chọn, đầu trang và chân trang

java15


  info.picocli
  picocli
  4.7.0
03

Kotlin


  info.picocli
  picocli
  4.7.0
04

Để biết thêm chi tiết, hãy xem bài viết này của Kiến trúc sư ngôn ngữ Java Brian Goetz

22. Tiêm phụ thuộc

22. 1. Ví dụ về hướng dẫn

Python chuyển sang ansi

Ví dụ dưới đây cho thấy cách tạo triển khai với Guice

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
128

Java


  info.picocli
  picocli
  4.7.0
05

Kotlin


  info.picocli
  picocli
  4.7.0
06

Sử dụng nhà máy tùy chỉnh khi tạo một phiên bản

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
777 hoặc khi gọi các phương thức thuận tiện
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
793 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794

Java


  info.picocli
  picocli
  4.7.0
07

Kotlin


  info.picocli
  picocli
  4.7.0
08

22. 2. Ví dụ khởi động mùa xuân

Python chuyển sang ansi

Kể từ phiên bản 4. 0, picocli đi kèm với hỗ trợ Spring Boot bằng cách cung cấp một nhà máy tùy chỉnh trong mô-đun

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
132

Ứng dụng ví dụ Spring Boot bên dưới cung cấp giao diện dòng lệnh cho ứng dụng thư khách có thể được sử dụng để gửi email bằng máy chủ SMTP. (Các) địa chỉ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
133 và dòng chủ đề có thể được cung cấp dưới dạng tùy chọn, trong khi nội dung thư có thể được chỉ định làm văn bản tham số

Để bắt đầu, hãy truy cập trang đầu tiên. Ứng dụng mẫu của chúng tôi có phụ thuộc

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
134, xin lưu ý rằng phụ thuộc này đã được chọn. Xem lại tất cả các cài đặt và thay đổi nếu cần, đặc biệt nếu bạn muốn viết mã ứng dụng của mình trong Kotlin. Bằng cách nhấp vào nút
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
135, bạn có thể tải xuống dự án Spring Boot khung xương độc lập, cùng với các phần phụ thuộc, tập lệnh xây dựng và các trường hợp thử nghiệm. Giải nén tệp lưu trữ vào thư mục bạn chọn và mở trình bao hoặc dấu nhắc lệnh cho thư mục này

Vì các phụ thuộc picocli không có sẵn trong Spring Initializr nên chúng tôi phải thêm chúng theo cách thủ công

maven


  info.picocli
  picocli
  4.7.0
09

Lớp (Groovy)


  info.picocli
  picocli
  4.7.0
10

Lớp (Kotlin)


  info.picocli
  picocli
  4.7.0
11

Điều này sẽ mang lại các phụ thuộc

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
136 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
137

Bây giờ hãy mở tệp nguồn được tạo trước

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
138, đổi tên nó thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
139 và chỉnh sửa cũng như mở rộng nó để nó trông như thế này

Java


  info.picocli
  picocli
  4.7.0
12

1Nhà máy được tiêm được tự động định cấu hình để tiêm

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
1402
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 được tiêm là lớp có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
142 của chúng tôi, được liệt kê tiếp theo

Kotlin


  info.picocli
  picocli
  4.7.0
13

1Nhà máy được tiêm được tự động định cấu hình để tiêm

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
1402
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 được tiêm là lớp có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
142 của chúng tôi, được liệt kê tiếp theo

Java


  info.picocli
  picocli
  4.7.0
14

1Chúng tôi chú thích lệnh của mình bằng chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
146 để Spring có thể tự động phát hiện lệnh đó để tiêm phụ thuộc. 2Logic nghiệp vụ của lệnh của bạn trông giống như bất kỳ lệnh picocli nào khác với các tùy chọn và tham số. 3Giao diện cho
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
147 được kết nối tự động của chúng tôi rất đơn giản

Kotlin


  info.picocli
  picocli
  4.7.0
15

1Chúng tôi chú thích lệnh của bạn bằng chú thích ________ 3146 để Spring có thể tự động phát hiện lệnh đó để tiêm phụ thuộc. 2Logic nghiệp vụ của lệnh của bạn trông giống như bất kỳ lệnh picocli nào khác với các tùy chọn và tham số. 3Giao diện cho

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
147 được kết nối tự động của chúng tôi rất đơn giản

Java


  info.picocli
  picocli
  4.7.0
16

Kotlin


  info.picocli
  picocli
  4.7.0
17

Và đây là việc triển khai dịch vụ thư của chúng tôi

Java


  info.picocli
  picocli
  4.7.0
18

1 ________ 3150 được cung cấp bởi Spring framework. 2Sau khi thư được gửi thành công, một thông báo tường trình sẽ được phát ra

Kotlin


  info.picocli
  picocli
  4.7.0
19

1 ________ 3150 được cung cấp bởi Spring framework. 2Sau khi thư được gửi thành công, một thông báo tường trình sẽ được phát ra

Cuối cùng, chúng tôi phải định cấu hình dịch vụ thư của mình. Điều này có thể được thực hiện bên trong tệp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
152, mà chúng tôi đặt trong thư mục con
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
153 của dự án của bạn

ứng dụng. của cải


  info.picocli
  picocli
  4.7.0
20

Với tất cả các lớp và cấu hình của chúng tôi, giờ đây chúng tôi có thể sử dụng trình bao bọc maven

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
154 để biên dịch và chạy ứng dụng mẫu của chúng tôi


  info.picocli
  picocli
  4.7.0
21

Vì chúng tôi không chỉ định đối số dòng lệnh, Picocli phàn nàn về tùy chọn bắt buộc bị thiếu

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
155 và đưa ra trợ giúp sử dụng. Chúng tôi phải chuyển các đối số dòng lệnh vào lệnh gọi dòng lệnh của mình, chúng tôi có thể sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
156 cho mục đích đó


  info.picocli
  picocli
  4.7.0
22

Với tất cả các tùy chọn và tham số được chỉ định, thư được gửi thành công

Tùy thuộc vào cấu hình của bạn, Spring có thể sử dụng proxy động cho các phiên bản được quản lý. Đây chắc chắn là trường hợp khi sử dụng các tính năng như và Spring (AOP), nhưng cũng có thể xảy ra trong các cấu hình khác

Điều an toàn nhất để làm trong các ứng dụng Spring là sử dụng công khai cho các chú thích của picocli's


  info.picocli
  picocli
  4.7.0
89 và

  info.picocli
  picocli
  4.7.0
90,

  info.picocli
  picocli
  4.7.0
92 và các chú thích khác. Việc sử dụng chú thích picocli trên các trường hoặc trên các phương thức không công khai có thể dẫn đến các giá trị được đưa vào picocli không khả dụng trong phiên bản được ủy quyền

README picocli-spring-boot-starter có một ví dụ khác về ứng dụng picocli được tích hợp vào Spring Boot

Có thể là một ý kiến ​​hay khi xác định một tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
161 trong lệnh của bạn. Spring Boot cho phép người dùng cuối chỉ định thuộc tính môi trường Spring
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
162 làm tùy chọn dòng lệnh để chỉ định vị trí thay thế cho tệp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
152. Việc xác định tùy chọn này ngăn picocli ném một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
352 ("Tùy chọn không xác định") khi nó thấy một tùy chọn mà nó không khớp. Bạn có thể biến nó thành tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
633 để nó không hiển thị trong thông báo trợ giúp sử dụng hoặc thêm mô tả giải thích ý nghĩa của nó

Ngoài ra, bạn có thể xác định một trường để nắm bắt tất cả các tùy chọn không xác định (và tham số vị trí)

Java


  info.picocli
  picocli
  4.7.0
23

Kotlin


  info.picocli
  picocli
  4.7.0
24

22. 3. Ví dụ vi phi hành gia

Khung dịch vụ vi mô Micronaut cung cấp cho picocli với lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
166 của nó

Ứng dụng ví dụ micronaut bên dưới cung cấp giao diện dòng lệnh cho ứng dụng khách HTTP có thể được sử dụng để truy xuất số lượng sao cho dự án GitHub thông qua API GitHub REST. Chủ sở hữu và tên (được gọi là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
167) của dự án được truy vấn có thể được cung cấp dưới dạng tham số dòng lệnh

Để bắt đầu, khung Micronaut trước tiên. Bây giờ bạn có thể sử dụng giao diện dòng lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
168 để tạo dự án đầu tiên của mình.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
169


  info.picocli
  picocli
  4.7.0
25

Lệnh này sẽ tạo một dự án micronaut độc lập với sự hỗ trợ của picocli, cùng với các phần phụ thuộc của nó, một tập lệnh xây dựng lớp và các trường hợp thử nghiệm. Đừng quên tùy chọn

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
170, tùy chọn này sẽ thêm phần phụ thuộc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
171 vào dự án. Sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
172 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
173 để tạo mã cho các ngôn ngữ khác. Mở tệp nguồn được tạo tự động
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
174 và thay đổi cũng như mở rộng nó để nó trông như thế này

Java


  info.picocli
  picocli
  4.7.0
26

Kotlin


  info.picocli
  picocli
  4.7.0
27

Bây giờ bạn có thể chạy ứng dụng bằng công cụ xây dựng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
175


  info.picocli
  picocli
  4.7.0
28

Sử dụng giao diện dòng lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
168, bạn có thể thêm một lệnh khác (bao gồm cả trường hợp thử nghiệm) vào dự án của mình


  info.picocli
  picocli
  4.7.0
29

Để chạy các trường hợp thử nghiệm được tạo tự động cho lớp đã tạo trước đó, chỉ cần đưa ra


  info.picocli
  picocli
  4.7.0
30

Tích hợp Picocli của Micronaut có nhiều thông tin hơn, bao gồm phần mở rộng

22. 4. Ví dụ Quarkus

Python chuyển sang ansi

quarkus 1. 5 đã thêm hỗ trợ cho các ứng dụng, cho phép sử dụng Picocli trong các ứng dụng Quarkus một cách dễ dàng

Ứng dụng ví dụ Quarkus bên dưới cung cấp giao diện dòng lệnh cho ứng dụng thư khách có thể được sử dụng để gửi email bằng máy chủ SMTP. Các địa chỉ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
177 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
133 phải được cung cấp dưới dạng tùy chọn, trong khi nội dung thư có thể được chỉ định làm văn bản tham số. Lệnh phụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
179 có thể được sử dụng để chỉ định một hoặc nhiều dòng tiêu đề tùy chỉnh

Để bắt đầu, trước tiên hãy truy cập Quarkus Web Application Configurator. Ứng dụng ví dụ của chúng tôi phụ thuộc vào các tiện ích mở rộng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
180 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
181, xin lưu ý rằng các tiện ích mở rộng này đã được chọn rồi. Nếu bạn muốn viết mã ứng dụng của mình trong Kotlin, bạn cũng cần thêm tiện ích mở rộng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
182. Bây giờ di chuột qua nút
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
183 và chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
184. Điều này sẽ tải xuống một dự án Quarkus khung độc lập với sự hỗ trợ của picocli, cùng với các phụ thuộc của nó, một tập lệnh xây dựng maven và các trường hợp thử nghiệm. Giải nén tệp lưu trữ vào thư mục bạn chọn và mở trình bao hoặc dấu nhắc lệnh cho thư mục này. Bây giờ hãy mở tệp nguồn được tạo trước
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
185, đổi tên nó thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
186 và chỉnh sửa cũng như mở rộng nó để nó trông như thế này

Java


  info.picocli
  picocli
  4.7.0
31

1 Lớp cha ________ 3141 đóng vai trò là điểm vào cho dòng lệnh picocli. 2 Lớp tĩnh bên trong ________ 3188 đại diện cho lệnh con ________ 3179. 3Vì cả lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 và lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
188 đều được chú thích bằng chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
192, nên có sự không rõ ràng về điểm vào của ứng dụng. Điều này được giải quyết bằng cách chú thích lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
194. Thay vào đó, sự mơ hồ này cũng có thể được giải quyết bằng cách chỉ định thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
195. Thuộc tính này được ưu tiên hơn chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
196

Kotlin


  info.picocli
  picocli
  4.7.0
32

1 Lớp cha ________ 3141 đóng vai trò là điểm vào cho dòng lệnh picocli. 2 Lớp ________ 3188 bên trong đại diện cho lệnh con ________ 3179. 3Vì cả lớp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 và lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
188 đều được chú thích bằng chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
192, nên có sự không rõ ràng về điểm vào của ứng dụng. Điều này được giải quyết bằng cách chú thích lớp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
141 với chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
194. Thay vào đó, sự mơ hồ này cũng có thể được giải quyết bằng cách chỉ định thuộc tính
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
195. Thuộc tính này được ưu tiên hơn chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
196

Sau khi mã hóa xong, bạn có thể sử dụng trình bao bọc maven

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
154 để biên dịch và chạy ứng dụng mẫu của chúng tôi


  info.picocli
  picocli
  4.7.0
33

Vì chúng tôi không chỉ định đối số dòng lệnh, Picocli phàn nàn về các tùy chọn bắt buộc bị thiếu

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
208 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
155 và đưa ra trợ giúp sử dụng. Chúng tôi phải chuyển các đối số dòng lệnh vào lệnh gọi dòng lệnh của mình, chúng tôi có thể sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
210 cho mục đích đó


  info.picocli
  picocli
  4.7.0
34

Với tất cả các tùy chọn và tham số bắt buộc được chỉ định, thư được gửi thành công

Gửi email trong hệ thống phát triển và sản xuất

Nếu bạn đang chạy Quarkus ở chế độ DEV hoặc TEST, thư không thực sự được gửi đi mà được in trên

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
211 và được thu thập trong một đậu
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
212 thay thế. Trong một hệ thống sản xuất, bạn phải đặt giá trị cấu hình boolean
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
213 thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248. Ngoài ra, bạn cần cung cấp thêm các giá trị cấu hình (tên máy chủ SMTP, … bên trong tệp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
215. Vui lòng tham khảo hướng dẫn Gửi email của Quarkus để biết chi tiết và thông tin bổ sung

Ngoài ra, giao diện dòng lệnh của chúng tôi cung cấp cơ hội chỉ định một hoặc nhiều dòng tiêu đề tùy chỉnh thông qua lệnh phụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
179, cụ thể hơn là thông qua tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
217 (
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
218) của lệnh phụ này


  info.picocli
  picocli
  4.7.0
35

Lệnh này sẽ thêm hai dòng tiêu đề tùy chỉnh bổ sung vào thư của chúng tôi


  info.picocli
  picocli
  4.7.0
36

1Dòng tiêu đề tùy chỉnh 1, bắt đầu bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
219 như được định nghĩa trong RFC 8222Dòng tiêu đề tùy chỉnh 2

Hướng dẫn Quarkus cho chế độ Lệnh với Picocli đã mở rộng thông tin về tích hợp Quarkus Picocli

22. 5. CDI 2. 0 (JSR 365)

Nếu ứng dụng của bạn chạy trong vùng chứa nội dung tiêm phụ thuộc không cung cấp hỗ trợ rõ ràng cho picocli, thì vẫn có thể nhận nội dung phụ thuộc trong các lệnh picocli, lệnh con và các lớp khác như nhà cung cấp phiên bản, nhà cung cấp mặc định, trình chuyển đổi loại, v.v.

Về bản chất, tất cả những gì cần thiết là triển khai giao diện

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
083 lấy các đối tượng từ vùng chứa nội xạ phụ thuộc thay vì khởi tạo chúng trực tiếp

Dưới đây là một nhà máy CDI ví dụ hoạt động cho bất kỳ vùng chứa nào triển khai Nội dung chèn ngữ cảnh và phụ thuộc cho Java 2. 0 đặc điểm kỹ thuật (JSR 365)


  info.picocli
  picocli
  4.7.0
37

Cách sử dụng nhà máy trên tùy thuộc vào thùng chứa cụ thể, nhưng nhìn chung nó sẽ trông giống như bên dưới


  info.picocli
  picocli
  4.7.0
38

23. Mô-đun Java 9 JPMS

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
221 chính là một mô-đun JPMS có tên là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
222

Bắt đầu từ picocli 4. 0, jar này sẽ là một mô-đun rõ ràng thay vì một mô-đun tự động, vì vậy công cụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
223 có thể được sử dụng để cung cấp một hình ảnh nhị phân được cắt chỉ có các mô-đun cần thiết. (Lưu ý rằng có nhiều cách để sử dụng jlink với các ứng dụng không theo mô-đun. )

Thông thường, một jar mô-đun bao gồm tệp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
224 trong thư mục gốc của nó. Điều này gây ra sự cố cho một số công cụ cũ hơn, xử lý không chính xác bộ mô tả mô-đun như thể nó là một lớp Java bình thường. Để cung cấp khả năng tương thích ngược tốt nhất, tạo tác picocli chính là a với tệp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
224 nằm trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
226

23. 1. Cấu hình mô-đun

Các ứng dụng sử dụng các mô-đun của Java 9 cần định cấu hình mô-đun của chúng để cho phép truy cập phản xạ picocli vào các lớp và trường được chú thích

Các ứng dụng thường muốn các lớp và trường được chú thích ở chế độ riêng tư; . Các cài đặt bên dưới làm cho điều này có thể

Ví dụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
227


  info.picocli
  picocli
  4.7.0
39

Lưu ý rằng không có gói nào là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
228, vì vậy các mô-đun khác không thể vô tình biên dịch theo các loại trong các gói này

Ngoài ra


  info.picocli
  picocli
  4.7.0
40

24. Gói OSGi

Python chuyển sang ansi

từ 4. 0, picocli JAR là gói OSGi có

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
229 và siêu dữ liệu phù hợp khác trong tệp kê khai

25. truy tìm

Picocli 1. 0 đã giới thiệu hỗ trợ theo dõi trình phân tích cú pháp để tạo điều kiện khắc phục sự cố

Các mức được hỗ trợ là ________ 3230, ________ 3231, ________ 3232 và ________ 3233. Mức theo dõi mặc định là

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
231

  • GỠ LỖI. Hiển thị chi tiết về các quyết định do trình phân tích cú pháp đưa ra trong quá trình phân tích cú pháp dòng lệnh

  • THÔNG TIN. Hiển thị tổng quan cấp cao về những gì xảy ra trong quá trình phân tích cú pháp dòng lệnh

  • CẢNH BÁO. mặc định. Hiển thị cảnh báo thay vì lỗi khi bật phân tích cú pháp nhẹ nhàng. khi các tùy chọn một giá trị được chỉ định nhiều lần (và

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    235 là
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    248) hoặc khi không thể khớp các đối số dòng lệnh dưới dạng một tùy chọn hoặc tham số vị trí (và
    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    237 là
    import picocli.CommandLine;
    import picocli.CommandLine.Command;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.security.MessageDigest;
    import java.util.concurrent.Callable;
    
    @Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
             description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
    class CheckSum implements Callable<Integer> {
    
        @Parameters(index = "0", description = "The file whose checksum to calculate.")
        private File file;
    
        @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
        private String algorithm = "SHA-256";
    
        @Override
        public Integer call() throws Exception { // your business logic goes here...
            byte[] fileContents = Files.readAllBytes(file.toPath());
            byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
            System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
            return 0;
        }
    
        // this example implements Callable, so parsing, error handling and handling user
        // requests for usage help or version help can be done with one line of code.
        public static void main(String... args) {
            int exitCode = new CommandLine(new CheckSum()).execute(args);
            System.exit(exitCode);
        }
    }
    248)

  • TẮT. Loại bỏ tất cả dấu vết bao gồm cả cảnh báo

25. 1. Cấp độ theo dõi thông qua thuộc tính hệ thống

Thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
239 có thể được sử dụng để kiểm soát mức theo dõi

Chỉ định thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
240 mà không có giá trị sẽ đặt mức theo dõi thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
232

Thí dụ


  info.picocli
  picocli
  4.7.0
41

đầu ra


  info.picocli
  picocli
  4.7.0
42

25. 2. API theo dõi

Từ picocli 4. 7. 0, các ứng dụng có thể lập trình thiết lập cấp theo dõi và sử dụng theo dõi trong các thành phần tùy chỉnh

Ngoài việc thiết lập thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
239, giờ đây các ứng dụng có thể thay đổi mức theo dõi thông qua phương pháp
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
243. Ví dụ


  info.picocli
  picocli
  4.7.0
43

Phương thức công khai mới

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
244 trả về đối tượng đơn lẻ
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
245 được picocli sử dụng nội bộ và cũng có thể được sử dụng bởi các triển khai thành phần tùy chỉnh để thực hiện theo dõi. Ví dụ


  info.picocli
  picocli
  4.7.0
44

26. Tự động điền TAB

Các ứng dụng dựa trên Picocli hiện có thể hoàn thành dòng lệnh trong Bash hoặc Zsh Unix shell. Xem hướng dẫn Tự động hoàn thành cho Ứng dụng dòng lệnh Java để biết cách tạo tập lệnh tự động hoàn thành phù hợp với ứng dụng của bạn

27. Tạo tài liệu trang người đàn ông

Từ picocli 4. 2, mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 có công cụ
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
247 có thể tạo tài liệu AsciiDoc bằng cách sử dụng cấu trúc tài liệu trang và loại tài liệu
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
248. Các tệp AsciiDoc đã tạo có thể được chuyển đổi thành các trang man HTML, PDF và unix bằng công cụ tuyệt vời

Xem phần của picocli-codegen README để biết chi tiết về cách tạo và tùy chỉnh tài liệu này

Để xem một số ví dụ, các trang hướng dẫn cho các công cụ tích hợp picocli đã được tạo bằng công cụ này

  • trang-gen (

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    249)

  • picocli. Tự động hoàn tất (

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    250)

  • tạo-hoàn thành (______3251 - được sử dụng làm lệnh con)

  • gen-refl-config (______3252)

  • gen-proxy-config (

    @Grab('info.picocli:picocli-groovy:4.7.0')
    import static picocli.CommandLine.*
    import groovy.transform.Field
    import java.security.MessageDigest
    
    @Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
      description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
    @picocli.groovy.PicocliScript
    
    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    @Field File file
    
    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    @Field String algorithm = 'SHA-256'
    
    println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
    253)

  • gen-resource-config (______3254)

Từ picocli 4. 4, công cụ này có thể được sử dụng như một trong ứng dụng của bạn, với cú pháp thông thường


  info.picocli
  picocli
  4.7.0
45

Để sử dụng công cụ

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
247 như một lệnh con, bạn sẽ cần có tệp jar
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 trong đường dẫn lớp của mình

28. Kiểm tra ứng dụng của bạn

28. 1. Kiểm thử hộp đen và hộp trắng

Picocli 4. 0 đã giới thiệu các API ,

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
258 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
259 giúp việc kiểm tra dễ dàng hơn rất nhiều

Các ứng dụng có thể thực hiện kiểm tra hộp đen bằng cách xác minh mã thoát và đầu ra của chương trình thành luồng đầu ra tiêu chuẩn và luồng lỗi tiêu chuẩn

Ngoài ra, bạn có thể thực hiện kiểm tra hộp trắng bằng cách giữ tham chiếu đến ứng dụng và xác nhận trạng thái của ứng dụng sau khi cung cấp cho ứng dụng các đầu vào dòng lệnh khác nhau

Ví dụ

Java


  info.picocli
  picocli
  4.7.0
46

Kotlin


  info.picocli
  picocli
  4.7.0
47

Điều này giả định rằng ứng dụng sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
260 được cung cấp bởi hoặc. Các ứng dụng có thể nhận được những người viết này thông qua chú thích

Java


  info.picocli
  picocli
  4.7.0
48

Kotlin


  info.picocli
  picocli
  4.7.0
49

28. 2. Kiểm tra đầu ra

Các ứng dụng in trực tiếp tới

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
264 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
265 hoặc sử dụng phiên bản picocli thấp hơn 4. 0, có thể được kiểm tra bằng cách ghi lại các luồng lỗi và đầu ra tiêu chuẩn. Có nhiều tùy chọn khác nhau để thực hiện việc này, một số trong số đó được hiển thị bên dưới

28. 2. 1. Java 8+ với System-Lambda

Các ứng dụng sử dụng JUnit 5 hoặc Java 8+ có thể sử dụng dự án System-Lambda của Stephan Birkner để thu thập đầu ra. Thư viện này có các phương tiện để ghi lại các luồng lỗi tiêu chuẩn và đầu ra tiêu chuẩn một cách riêng biệt hoặc trộn lẫn với nhau và để chuẩn hóa các ngắt dòng

Ví dụ sử dụng


  info.picocli
  picocli
  4.7.0
50

28. 2. 2. JUnit 4 với Quy tắc hệ thống

Các ứng dụng sử dụng JUnit 4 có thể sử dụng dự án Quy tắc hệ thống của Stephan Birkner để thu thập đầu ra

Ví dụ sử dụng


  info.picocli
  picocli
  4.7.0
51

28. 2. 3. Chụp đầu ra DIY

Cũng có thể tự chụp đầu ra trong các bài kiểm tra mà không cần sử dụng thư viện

Ví dụ


  info.picocli
  picocli
  4.7.0
52

Điều này có vẻ dài dòng, nhưng có thể được đơn giản hóa rất nhiều bằng cách để khung thử nghiệm thực hiện thiết lập và dọn dẹp

JUnit 4 cho phép bạn thiết lập theo phương thức có chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
266 và chia nhỏ theo phương thức có chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
267. Các chú thích tương đương JUnit 5 lần lượt là
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
268 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
269. Ví dụ


  info.picocli
  picocli
  4.7.0
53

28. 3. Kiểm tra mã thoát

Kiểm tra mã thoát của các ứng dụng gọi

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201 có thể phức tạp nhưng không phải là không thể. Tùy thuộc vào ngôn ngữ lập trình, môi trường và khung thử nghiệm của bạn, bạn có một số tùy chọn

  • java8. Bạn có thể sử dụng dự án System-Lambda của Stephan Birkner để thử nghiệm

  • Java/Kotlin + Junit 5. Bạn có thể sử dụng Hệ thống của Todd Ginsberg. tiện ích mở rộng exit() cho ngày 5 tháng 6. Dự án README đề cập chung

  • Java + Junit 4. Bạn có thể sử dụng dự án Quy tắc hệ thống của Stephan Birkner để kiểm tra

  • Kotlin + Kotest. Lập trình viên Kotlin bạn có thể sử dụng khung kiểm tra Kotest đi kèm cho phép bạn kiểm tra mã thoát

Danh sách dưới đây cung cấp các mẫu mã cho tất cả bốn tùy chọn được đề cập ở trên

java8


  info.picocli
  picocli
  4.7.0
54

Java/Tháng 6 5


  info.picocli
  picocli
  4.7.0
55

Java/Junt 4


  info.picocli
  picocli
  4.7.0
56

Kotlin/Kotest


  info.picocli
  picocli
  4.7.0
57

28. 4. Kiểm tra các biến môi trường

Vì picocli cung cấp hỗ trợ để sử dụng trong các chú thích, bạn có thể muốn kiểm tra điều này. Tùy thuộc vào ngôn ngữ lập trình, môi trường và khung thử nghiệm của bạn, bạn có một số tùy chọn

  • java8. Bạn có thể sử dụng dự án System-Lambda của Stephan Birkner để thử nghiệm

  • Java + Junit 4. Bạn có thể sử dụng dự án Quy tắc hệ thống của Stephan Birkner để kiểm tra

  • Kotlin + Kotest. Lập trình viên Kotlin, bạn có thể sử dụng khung kiểm tra Kotest đi kèm với tiện ích mở rộng cho phép bạn kiểm tra các biến môi trường

Danh sách dưới đây cung cấp các mẫu mã cho ba tùy chọn được đề cập ở trên

java8


  info.picocli
  picocli
  4.7.0
58

Java/JUnit 4


  info.picocli
  picocli
  4.7.0
59

Kotlin/Kotest


  info.picocli
  picocli
  4.7.0
60

Các phiên bản mới nhất của

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
271, 1. 18 và 1. 19, có sự cố nghĩa là quy tắc Biến môi trường sẽ không hoạt động trong JUnit 5. Sử dụng Quy tắc hệ thống phiên bản 1. 17. 2 thay vì khi kết hợp với JUnit 5

28. 5. Chế giễu

Nếu bạn thích sử dụng chế độ mô phỏng trong các bài kiểm tra của mình, thì có sẵn một số khung. Tài liệu này sẽ chỉ đề cập đến một số khía cạnh có liên quan của một số khung mô phỏng dựa trên phản hồi từ cộng đồng picocli

28. 5. 1. Mocking các tiểu ban với Mockito

Khung Mockito tạo một lớp tổng hợp cho một lớp giả định. Lớp được tạo là một lớp con của lớp mô phỏng và cũng sao chép tất cả các chú thích vào lớp mô phỏng. Nếu bạn đang chế nhạo một lệnh bằng các lệnh con, điều này có thể dẫn đến lỗi

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
272. Để giảm thiểu điều này, hãy định cấu hình mô phỏng để không sao chép chú thích. Ví dụ dưới đây cho thấy cách sử dụng MockSettings API để thực hiện điều này

Thí dụ


  info.picocli
  picocli
  4.7.0
61

Bạn không thể sử dụng MockSettings với

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
273. Để giả lập các lệnh với các lệnh con, bạn sẽ cần sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
274 thay vì
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
273

29. Đóng gói ứng dụng của bạn

Bạn đã hoàn thành ứng dụng của mình. Xin chúc mừng. Bây giờ, làm cách nào để bạn đóng gói và phân phối nó cho người dùng của mình?

Như đã đề cập trong phần trước của sách hướng dẫn này, một cách để chạy ứng dụng của chúng ta là như thế này


  info.picocli
  picocli
  4.7.0
62

Điều đó khá dài dòng. Bạn có thể muốn đóng gói ứng dụng của mình theo cách mà người dùng cuối có thể gọi nó bằng tên lệnh của nó như thế này


  info.picocli
  picocli
  4.7.0
63

Xét cho cùng, phần trợ giúp sử dụng hiển thị tên lệnh được xác định trong

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
276, vì vậy chúng tôi muốn người dùng có thể chạy ứng dụng bằng lệnh này

Dưới đây là một số ý tưởng về cách thực hiện điều này

29. 1. bí danh

Trên các hệ điều hành dựa trên unix, bạn có thể yêu cầu người dùng của mình xác định bí danh. Ví dụ


  info.picocli
  picocli
  4.7.0
64

Nối dòng trên vào tệp

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
277 của bạn để cung cấp bí danh này trong mọi phiên trình bao mới

Điều này yêu cầu JRE được cài đặt trên máy và giả định rằng tệp thực thi

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278 nằm trong tệp

Trong Windows, lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
280 có thể được sử dụng để xác định macro, nhưng lệnh này có

Xác định bí danh là rẻ và dễ dàng, nhưng mong manh và không dễ di chuyển. Một cách khác tốt hơn là tạo tập lệnh launcher. Nhiều công cụ xây dựng cung cấp plugin để tạo tập lệnh trình khởi chạy cho ứng dụng của bạn, như được trình bày chi tiết trong phần sau

29. 2. Tập lệnh khởi chạy

Plugin Maven Trình biên dịch ứng dụng có thể được sử dụng với Maven và tương tự, plugin Ứng dụng Gradle có thể được sử dụng với Gradle, để tạo tệp zip hoặc tệp tar phân phối với tập lệnh trình khởi chạy

Cả hai đều yêu cầu cài đặt JRE trên máy đích

Một giải pháp thú vị hơn nữa có thể là tạo một hình ảnh gốc GraalVM cho ứng dụng của bạn, như được trình bày chi tiết trong phần sau

29. 3. Hình ảnh bản địa GraalVM

Python chuyển sang ansi

29. 3. 1. Hình ảnh gốc GraalVM là gì?

GraalVM Native Image cho phép bạn biên dịch trước mã Java thành một tệp thực thi độc lập, được gọi là ảnh gốc. Kết quả thực thi bao gồm ứng dụng, thư viện và JDK và không yêu cầu cài đặt máy ảo Java riêng biệt. Hình ảnh gốc được tạo có thời gian khởi động nhanh hơn và chi phí bộ nhớ thời gian chạy thấp hơn so với máy ảo Java

Hình ảnh gốc của GraalVM có một số hạn chế và yêu cầu một số cấu hình bổ sung để có thể sử dụng các tính năng như phản chiếu, tài nguyên (bao gồm cả gói tài nguyên) và proxy động

29. 3. 2. Làm cách nào để tạo Ảnh gốc cho Ứng dụng của tôi?

Mô-đun

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
205 chứa bộ xử lý chú thích tạo ra các tệp cấu hình cần thiết bên dưới
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
206 trong quá trình biên dịch, được đưa vào tệp ứng dụng. Phần của sách hướng dẫn có chi tiết về cách định cấu hình bộ xử lý chú thích và có thể tìm thấy thêm thông tin trong tệp README picocli-codegen

Bằng cách nhúng các tệp cấu hình này, jar của bạn ngay lập tức được kích hoạt Graal. cái được sử dụng để tạo tệp thực thi gốc có thể lấy cấu hình từ tệp jar. Trong hầu hết các trường hợp, không cần cấu hình thêm khi tạo hình ảnh gốc

Sau khi cài đặt GraalVM và cài đặt tiện ích trình tạo

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
283 (với
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
285), bạn có thể tạo một hình ảnh gốc bằng cách gọi lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
283


  info.picocli
  picocli
  4.7.0
65

Để tạo một hình ảnh gốc, chuỗi công cụ biên dịch cho nền tảng của bạn cần được cài đặt. Xem Xây dựng ứng dụng CLI bản địa tuyệt vời bằng Java với Graalvm và Picocli để biết chi tiết

GraalVM bao gồm một plugin Maven để tạo hình ảnh gốc trong quá trình xây dựng. Người dùng Gradle có thể quan tâm đến plugin gradle-graal của Palantir hoặc plugin graalvm-native-image của Mike Neck. (Tài liệu cho cả plugin Maven và Gradle có vẻ thưa thớt tại thời điểm viết bài này. )

29. 3. 3. Tôi có thể lấy thêm thông tin chi tiết ở đâu?

Bài viết InfoQ này Xây dựng các ứng dụng CLI gốc tuyệt vời trong Java bằng Graalvm và Picocli cung cấp chi tiết về cách thiết lập chuỗi công cụ GraalVM để tạo hình ảnh gốc. Nó đặc biệt chú ý đến Windows, nơi việc thiết lập chuỗi công cụ biên dịch có thể phức tạp

Bài viết cũ hơn này, "Picocli trên GraalVM. Blazingly Fast Command Line Apps", vẫn có thể hữu ích

Xem thêm dự án Gradle demo này và dự án Maven demo này cung cấp một ứng dụng CLI hoàn chỉnh đơn giản, với các hướng dẫn xây dựng, mà các nhà phát triển có thể sao chép và xây dựng để có được tệp thực thi độc lập

Hình ảnh gốc trên Windows có vấn đề là hình ảnh gốc không bao gồm phụ thuộc bắt buộc

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
287 (đối với Java 8) hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
288 (với GraalVM 19. 3 trở lên cho Java 11). Trên các máy Windows không có phần phụ thuộc này trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
279, hình ảnh gốc không khởi động được và hiển thị hộp thoại báo lỗi "Lỗi hệ thống. Chương trình không thể bắt đầu vì VCRUNTIME140. dll bị thiếu trong máy tính của bạn"

Rất cám ơn Matthew Mellon đã sử dụng pefrmdllembed như một giải pháp thay thế. cú pháp


  info.picocli
  picocli
  4.7.0
66

Hãy cẩn thận cho cách giải quyết trên

Nếu bạn nhúng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
290 hoặc
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
291 vào exe của mình thay vì tạo trình cài đặt để cài đặt phần phụ thuộc, bạn có thể sẽ mất khả năng Windows áp dụng các bản cập nhật cho các lỗ hổng trong thời gian chạy được chia sẻ có thể ảnh hưởng đến tính bảo mật của ứng dụng của bạn. Trình cài đặt Windows có phép thuật giúp bạn không hạ cấp DLL trong
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
292 và sau khi được cài đặt, tôi tin rằng Windows Update sẽ giúp nó luôn cập nhật

29. 4. JAR thực sự có thể thực thi

Một tệp JAR thực sự có thể thực thi được là một tệp JAR cũng là một tập lệnh trình bao thực thi JAR bằng cách chạy nó với

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
278. Do đó, nó hoạt động như một tệp thực thi, có thể di động trên các hệ điều hành và kiến ​​trúc và vẫn là một tệp JAR hợp lệ có thể được thực thi một cách rõ ràng hoặc được sử dụng như một tệp JAR

Có thể sử dụng Plugin JAR Maven thực sự có thể thực thi được để tạo một JAR như vậy, thường là kết hợp với plugin bóng râm, vì JAR cần phải độc lập, chứa tất cả các phụ thuộc của nó

29. 5. ra mắt4j

launch4j có thể tạo trình khởi chạy gốc có thể thực thi được. Nó có thể đóng gói một JRE (~200MB) hoặc sử dụng một cái được cài đặt sẵn

29. 6. gói java (Java 8)

JDK 8 javapackager có thể tạo trình khởi chạy gốc có thể thực thi được. Nó có thể đóng gói một JRE (~200MB) hoặc sử dụng một cái được cài đặt sẵn

Với hệ thống mô-đun JPMS được giới thiệu trong Java 9, có thể sử dụng để tạo một JRE trọng lượng nhẹ tùy chỉnh chỉ với các mô-đun cần thiết. JRE này có thể nhỏ tới 30-40MB. Sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
223 với tùy chọn
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
295 để tạo tập lệnh trình khởi chạy cho ứng dụng của bạn

Xem bài viết này để sử dụng jlink cho các ứng dụng không theo mô-đun

Người dùng Gradle có thể thích các plugin này. tổ chức. beryx Badass Runtime Plugin và tổ chức. beryx Trình cắm Badass JLink

29. 8. gói j

jpackage là một công cụ đóng gói theo JEP 343 đi kèm với JDK 14. JPackage có thể sử dụng JRE trọng lượng nhẹ tùy chỉnh do

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
223 sản xuất và tạo trình cài đặt cũng như trình khởi chạy ứng dụng gốc. Kết quả là một "hình ảnh ứng dụng" Java là một thư mục duy nhất trong hệ thống tệp chứa trình khởi chạy ứng dụng gốc, các tệp cấu hình và tài nguyên cũng như hình ảnh thời gian chạy JRE trọng lượng nhẹ tùy chỉnh (bao gồm các mô-đun ứng dụng) do
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
223 tạo ra

Để biết thêm chi tiết, bạn có thể đọc bản tóm tắt jpackage hoặc nghiên cứu Hướng dẫn sử dụng công cụ đóng gói. Bài viết InfoQ này cũng có thể được quan tâm

29. 9. jbang

jbang cho phép bạn viết các tập lệnh bằng Java sử dụng các phụ thuộc bên ngoài. Bạn sử dụng nó với chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
298, tương tự như chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
299 trong Groovy

Sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
300 để tạo tập lệnh ban đầu với mẫu picocli. Bản trình bày asciinema này cho thấy những gì có thể

29. 10. JRleaser

JReleaser cho phép một số tùy chọn đóng gói như Homebrew, Chocolatey, Snapcraft, Scoop, jbang, Docker, trong số những tùy chọn khác. Hiện tại hai loại phân phối khác nhau được hỗ trợ. jlink và phân phối zip/tar tiêu chuẩn. Cái sau có thể được tạo bằng sự kết hợp của các plugin appassembler-maven-plugin và maven-assembly-plugin nếu sử dụng Maven hoặc plugin ứng dụng nếu sử dụng Gradle

30. Picocli bằng các ngôn ngữ khác

Picocli có thể được sử dụng trong các ngôn ngữ JVM khác hỗ trợ chú thích

30. 1. hấp dẫn

30. 1. 1. Cú pháp chú thích Groovy

Trong Groovy, sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
612 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
613 để bao quanh các giá trị mảng, thay vì
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
303 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
304 được sử dụng trong Java


  info.picocli
  picocli
  4.7.0
67

30. 1. 2. Tập lệnh Groovy

Picocli 2. 0 đã giới thiệu hỗ trợ đặc biệt cho tập lệnh Groovy. Từ picocli 4. 0 cái này đã được chuyển vào một mô-đun riêng,

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
305. Trong picocli 4. 6,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
306 không được dùng để ủng hộ
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
307

Các tập lệnh được chú thích bằng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
308 được tự động chuyển đổi để sử dụng
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
309 làm lớp cơ sở của chúng và cũng có thể sử dụng chú thích

  info.picocli
  picocli
  4.7.0
91 để tùy chỉnh các phần của thông báo sử dụng như tên lệnh, mô tả, đầu trang, chân trang, v.v.

Trước khi phần thân tập lệnh được thực thi, lớp cơ sở

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
311 phân tích cú pháp dòng lệnh và khởi tạo các biến
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
312 được chú thích bằng

  info.picocli
  picocli
  4.7.0
89 hoặc

  info.picocli
  picocli
  4.7.0
90. Phần thân tập lệnh được thực thi nếu đầu vào của người dùng hợp lệ và không yêu cầu trợ giúp sử dụng hoặc thông tin phiên bản


  info.picocli
  picocli
  4.7.0
68

Chú thích

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
315 cũ hơn không được dùng nữa từ picocli 4. 6. Thay vào đó, các tập lệnh mới nên sử dụng chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
308 (và lớp cơ sở
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
309 được liên kết). Bảng dưới đây so sánh hai lớp cơ sở này

Bảng 6. So sánh các lớp cơ sở tập lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
311 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
319.
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
311
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
319

Các tiểu ban dưới dạng phương thức

Các lệnh con được hỗ trợ có thể được định nghĩa là các phương thức được chú thích bởi


  info.picocli
  picocli
  4.7.0
91 trong tập lệnh

không được hỗ trợ

Các lệnh con của

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
334

Được hỗ trợ, cả cái tích hợp và cái tùy chỉnh

không được hỗ trợ

mã thoát

được hỗ trợ. tập lệnh có thể ghi đè lên

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
324 để gọi
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
201

không được hỗ trợ

Thực thi lệnh

Thông qua lời kêu gọi của. Các tập lệnh có thể ghi đè lên

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
327 để cài đặt một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
218 tùy chỉnh

Thực thi sau khi phân tích cú pháp được xác định trong

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
329 và không dễ tùy chỉnh. Mọi lệnh con và lệnh chính đều được thực thi

Xử lý tùy chỉnh đầu vào của người dùng không hợp lệ

Các tập lệnh có thể ghi đè lên

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
327 để cài đặt một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
194 tùy chỉnh

Xử lý đầu vào không hợp lệ có thể được tùy chỉnh bằng cách ghi đè

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
332

Xử lý lỗi tùy chỉnh

Các tập lệnh có thể ghi đè lên

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
327 để cài đặt một
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
196 tùy chỉnh

Xử lý ngoại lệ thời gian chạy có thể được tùy chỉnh bằng cách ghi đè

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
335

Nguyên tắc

Triển khai

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
336, phần thân tập lệnh được chuyển thành phương thức
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
794

Nội dung tập lệnh được chuyển thành phương thức

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
338

Khi nâng cấp tập lệnh từ các phiên bản picocli cũ hơn 4. 0, chỉ thay đổi số phiên bản là không đủ. Tập lệnh nên sử dụng

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
339. Id tạo tác cũ
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
340 sẽ không hoạt động, vì lớp chú thích
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
315 và các lớp hỗ trợ đã được chuyển sang một mô-đun riêng biệt,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
305

30. 1. 3. Đóng cửa trong chú thích

Kể từ picocli 4. 6, Các chương trình Groovy có thể sử dụng các bao đóng trong các chú thích picocli thay vì chỉ định một lớp. Điều này có thể đặc biệt hữu ích trong các tập lệnh Groovy, nơi người ta không thể định nghĩa một lớp bên trong tĩnh

Thí dụ


  info.picocli
  picocli
  4.7.0
69

Khi một lớp được chỉ định, picocli tạo một thể hiện của lớp. Ngược lại, khi một bao đóng được chỉ định, picocli gọi bao đóng để lấy một thể hiện. (Nói chính xác, cả hai điều này đều được ủy quyền cho cấu hình và việc triển khai mặc định của nhà máy hỗ trợ các lần đóng từ picocli 4. 6. )

Như bạn có thể thấy trong ví dụ trên, mỗi bao đóng trong chú thích phải chứa một bao đóng khác có loại được yêu cầu (

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
344, v.v. )

1Lệnh

@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
551. lưu ý danh sách tham số trống trước mũi tên
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
346. Điều này là cần thiết để giúp trình biên dịch Groovy. Việc đóng cửa phải được đúc thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
554. 2Lệnh
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
348. trả về giá trị mặc định cho tham số
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
349 đã chỉ định. Việc đóng cửa phải được chuyển thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
344. 3Tùy chọn hoặc Tham số
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
351. trả về danh sách Chuỗi. Không có danh sách tham số hoặc truyền được yêu cầu. 4Tùy chọn hoặc Tham số
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
352. đưa ra một
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
353,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
349 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
282, xử lý các đối số còn lại. Việc đóng cửa phải được chuyển thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
243. 5Loại tùy chọn hoặc tham số
@Grab('info.picocli:picocli-groovy:4.7.0')
import picocli.CommandLine
import static picocli.CommandLine.*

import java.security.MessageDigest
import java.util.concurrent.Callable

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
class Checksum implements Callable<Integer> {

    @Parameters(index = '0', description = 'The file whose checksum to calculate.')
    File file

    @Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
    String algorithm = 'SHA-256'

    Integer call() throws Exception {
        println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
        0
    }

    static void main(String[] args) {
        System.exit(new CommandLine(new Checksum()).execute(args))
    }
}
772 có một loạt các bao đóng. hấp dẫn 3. 0. 7 hoặc cao hơn là bắt buộc. các phiên bản cũ hơn của Groovy bỏ qua các bao đóng trong chú thích mảng lớp. Mỗi lần đóng phải có một tham số và được chuyển thành
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
358

Từ phiên bản picocli 4. 7. 0, tính năng này có thể bị tắt bằng cách đặt thuộc tính hệ thống

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
359 thành
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
248. Tắt hỗ trợ đóng trong chú thích có thể cải thiện thời gian khởi động ứng dụng

30. 1. 4. Phiên bản cũ hơn của Groovy

Khi sử dụng phiên bản Groovy cũ hơn 2. 4. 7, hãy sử dụng giải pháp thay thế này cho lỗi Grape gây ra lỗi này.

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
361


  info.picocli
  picocli
  4.7.0
70

Để giúp bạn bắt đầu nhanh chóng, bạn có thể xem thư mục ví dụ Groovy của kho lưu trữ mã picocli

30. 2. Kotlin

30. 2. 1. Cú pháp chú thích Kotlin

Kotlin 1. 2 (phát hành ngày 28 tháng 11 năm 2017) chính thức hỗ trợ , cho phép ký hiệu nhỏ gọn hơn


  info.picocli
  picocli
  4.7.0
71

Khi chỉ định một lớp làm đối số của chú thích, hãy sử dụng lớp Kotlin (KClass). Trình biên dịch Kotlin sẽ tự động chuyển đổi nó thành một lớp Java, do đó mã Java sẽ có thể nhìn thấy các chú thích và đối số bình thường


  info.picocli
  picocli
  4.7.0
72

30. 2. 2. Phiên bản cũ hơn của Kotlin

Các phiên bản Kotlin trước 1. 2 không cho phép cú pháp ký tự mảng trong chú thích, vì vậy với các phiên bản Kotlin cũ hơn, bạn sẽ phải viết

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
362 cho các thuộc tính
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
224,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200


  info.picocli
  picocli
  4.7.0
73

Để giúp bạn bắt đầu nhanh chóng, bạn có thể xem qua thư mục ví dụ về Kotlin của kho lưu trữ mã picocli

30. 3. Scala

Scala không cho phép chỉ định thuộc tính chú thích mảng dưới dạng một giá trị, vì vậy hãy lưu ý rằng bạn sẽ phải viết

@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
366 cho các thuộc tính
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
224,
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
261 và
@Grab('info.picocli:picocli-groovy:4.7.0')
import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
  description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()
200


  info.picocli
  picocli
  4.7.0
74

Để giúp bạn bắt đầu nhanh chóng, bạn có thể xem thư mục ví dụ Scala của kho lưu trữ mã picocli

31. API JavaDoc

Picocli API JavaDoc có thể được tìm thấy ở đây

Xem thêm. Picocli 3. x JavaDoc

32. Dự án GitHub

Dự án GitHub có mã nguồn, thử nghiệm, xây dựng tập lệnh, v.v.

Sao và/hoặc rẽ nhánh dự án này trên GitHub nếu bạn thích nó.

33. Người tìm bệnh

Trình theo dõi vấn đề GitHub có thể được sử dụng để báo cáo lỗi hoặc yêu cầu các tính năng. Ngoài ra còn có một và đối với những câu hỏi mà cộng đồng người dùng có thể biết câu trả lời, StackOverflow vừa là một nguồn tài nguyên tốt vừa là một cách tuyệt vời để xây dựng cơ sở kiến ​​thức trực tuyến

34. Danh sách gửi thư

Tham gia nhóm Google picocli nếu bạn muốn thảo luận bất cứ điều gì liên quan đến picocli và nhận thông báo về các bản phát hành mới

35. Giấy phép

Picocli được cấp phép theo Giấy phép Apache 2. 0

36. phát hành

Các phiên bản trước có sẵn từ Bản phát hành dự án GitHub

37. Tải xuống

Bạn có thể thêm picocli làm phụ thuộc bên ngoài vào dự án của mình hoặc bạn có thể đưa nó làm nguồn

37. 1. Công cụ xây dựng

lớp


  info.picocli
  picocli
  4.7.0
75

Lớp (Kotlin)


  info.picocli
  picocli
  4.7.0
76

maven


  info.picocli
  picocli
  4.7.0

Scala SBT


  info.picocli
  picocli
  4.7.0
78

thường xuân


  info.picocli
  picocli
  4.7.0
79

Giống nho


  info.picocli
  picocli
  4.7.0
80

Leiningen


  info.picocli
  picocli
  4.7.0
81

người xây dựng


  info.picocli
  picocli
  4.7.0
82

JBang


  info.picocli
  picocli
  4.7.0
83

37. 2. Nguồn

Bằng cách sử dụng picocli ở dạng nguồn, bạn có thể tránh được sự phụ thuộc bên ngoài vào picocli. Picocli chỉ có một tệp nguồn. Dòng lệnh. java. Điều này tạo điều kiện bao gồm picocli trong dự án của bạn. chỉ cần sao chép và dán mã của tệp này vào một tệp có tên là

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.concurrent.Callable;

@Command(name = "checksum", mixinStandardHelpOptions = true, version = "checksum 4.0",
         description = "Prints the checksum (SHA-256 by default) of a file to STDOUT.")
class CheckSum implements Callable<Integer> {

    @Parameters(index = "0", description = "The file whose checksum to calculate.")
    private File file;

    @Option(names = {"-a", "--algorithm"}, description = "MD5, SHA-1, SHA-256, ...")
    private String algorithm = "SHA-256";

    @Override
    public Integer call() throws Exception { // your business logic goes here...
        byte[] fileContents = Files.readAllBytes(file.toPath());
        byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
        System.out.printf("%0" + (digest.length*2) + "x%n", new BigInteger(1, digest));
        return 0;
    }

    // this example implements Callable, so parsing, error handling and handling user
    // requests for usage help or version help can be done with one line of code.
    public static void main(String... args) {
        int exitCode = new CommandLine(new CheckSum()).execute(args);
        System.exit(exitCode);
    }
}
204, thêm nó vào dự án của bạn và tận hưởng