1 /* Copyright 2017 The TensorFlow Authors All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include <stdio.h>
17
18 #include "tensorflow/core/framework/op_gen_lib.h"
19 #include "tensorflow/core/lib/core/errors.h"
20 #include "tensorflow/core/lib/strings/str_util.h"
21 #include "tensorflow/core/platform/env.h"
22 #include "tensorflow/core/platform/init_main.h"
23 #include "tensorflow/core/util/command_line_flags.h"
24
25 namespace tensorflow {
26 namespace {
27
Run(int argc,char ** argv)28 int Run(int argc, char** argv) {
29 string FLAGS_in = "";
30 string FLAGS_out = "";
31 string FLAGS_fields = "description";
32
33 std::vector<Flag> flag_list = {
34 Flag("in", &FLAGS_in, "Input proto text (.pbtxt) file name"),
35 Flag("out", &FLAGS_out,
36 "Output multi-line proto text (.mlpbtxt) file name"),
37 Flag("fields", &FLAGS_fields, "Comma-separated list of field names")};
38
39 // Parse the command-line.
40 const string usage = Flags::Usage(argv[0], flag_list);
41 const bool parse_ok = Flags::Parse(&argc, argv, flag_list);
42 if (argc != 1 || !parse_ok) {
43 printf("%s", usage.c_str());
44 return 2;
45 }
46
47 // Parse the --fields option.
48 std::vector<string> fields =
49 str_util::Split(FLAGS_fields, ',', str_util::SkipEmpty());
50 if (fields.empty()) {
51 printf("--fields must be non-empty.\n%s", usage.c_str());
52 return 2;
53 }
54
55 port::InitMain(argv[0], &argc, &argv);
56
57 // Read the input file --in.
58 string in_contents;
59 Status s = ReadFileToString(Env::Default(), FLAGS_in, &in_contents);
60 if (!s.ok()) {
61 printf("Error reading file %s: %s\n", FLAGS_in.c_str(),
62 s.ToString().c_str());
63 return 1;
64 }
65
66 // Write the output file --out.
67 const string out_contents = PBTxtToMultiline(in_contents, fields);
68 s = WriteStringToFile(Env::Default(), FLAGS_out, out_contents);
69 if (!s.ok()) {
70 printf("Error writing file %s: %s\n", FLAGS_out.c_str(),
71 s.ToString().c_str());
72 return 1;
73 }
74
75 return 0;
76 }
77
78 } // namespace
79 } // namespace tensorflow
80
main(int argc,char ** argv)81 int main(int argc, char** argv) { return tensorflow::Run(argc, argv); }
82