ulatekh ggerganov commited on
Commit
07da1b5
·
unverified ·
1 Parent(s): 8cc6334

main : allow a response-file as the sole parameter (#2019)

Browse files

* The "main" example now allows a response-file as the sole parameter.

A response-file is a text file with command-line parameters, one per line.
Prefix the name of the response-file with "@" to identify it as such.
It's used under MS Windows to work around command-line length limits.
It may be useful under other platforms to simplify character-escaping.

* minor : style

---------

Co-authored-by: Georgi Gerganov <[email protected]>

Files changed (1) hide show
  1. examples/main/main.cpp +29 -0
examples/main/main.cpp CHANGED
@@ -867,6 +867,35 @@ void cb_log_disable(enum ggml_log_level , const char * , void * ) { }
867
  int main(int argc, char ** argv) {
868
  whisper_params params;
869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
870
  if (whisper_params_parse(argc, argv, params) == false) {
871
  whisper_print_usage(argc, argv, params);
872
  return 1;
 
867
  int main(int argc, char ** argv) {
868
  whisper_params params;
869
 
870
+ // If the only argument starts with "@", read arguments line-by-line
871
+ // from the given file.
872
+ std::vector<std::string> vec_args;
873
+ if (argc == 2 && argv != nullptr && argv[1] != nullptr && argv[1][0] == '@') {
874
+ // Save the name of the executable.
875
+ vec_args.push_back(argv[0]);
876
+
877
+ // Open the response file.
878
+ char const * rspfile = argv[1] + sizeof(char);
879
+ std::ifstream fin(rspfile);
880
+ if (fin.is_open() == false) {
881
+ fprintf(stderr, "error: response file '%s' not found\n", rspfile);
882
+ return 1;
883
+ }
884
+
885
+ // Read the entire response file.
886
+ std::string line;
887
+ while (std::getline(fin, line)) {
888
+ vec_args.push_back(line);
889
+ }
890
+
891
+ // Use the contents of the response file as the command-line arguments.
892
+ argc = static_cast<int>(vec_args.size());
893
+ argv = static_cast<char **>(alloca(argc * sizeof (char *)));
894
+ for (int i = 0; i < argc; ++i) {
895
+ argv[i] = const_cast<char *>(vec_args[i].c_str());
896
+ }
897
+ }
898
+
899
  if (whisper_params_parse(argc, argv, params) == false) {
900
  whisper_print_usage(argc, argv, params);
901
  return 1;