Command line function code generation method, device and computer readable storage medium
By automatically generating command-line function code and utilizing modeling input files and trie data structures, the problem of high workload and error rate in manually writing command-line code is solved, thus achieving efficient SDK development.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- 北京晟芯网络科技有限公司
- Filing Date
- 2022-11-29
- Publication Date
- 2026-07-21
AI Technical Summary
In the process of SDK development, manually writing command-line code is labor-intensive, error-prone, and inefficient, especially in the determination of parameter numbers and required options, which can easily lead to errors and low development efficiency.
The logic for generating code is extracted from the modeling input file using a modeling approach. The program that abstracts the command line generation logic automatically generates command line function code. The trie data structure is used to process non-fixed characters and generate comments, function headers, and command fields for the command line functions, thus avoiding human error.
It automatically generates multiple command-line function codes within seconds, improving development efficiency, avoiding errors that may occur in manual coding, and simplifying the SDK development process.
Smart Images

Figure CN115794057B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and more specifically, to a method, apparatus, and computer-readable storage medium for generating command-line function code. Background Technology
[0002] In the development of a Software Development Kit (SDK), the software primarily provides Application Programming Interfaces (APIs) for customers. Developing APIs requires writing command-line code for API verification. Command lines are a method of calling APIs by parsing strings. Using command lines allows for overriding the register read / write operations encapsulated by the API, and can be used to verify register functionality. A portion of API development time is spent writing this command-line code; only when the corresponding command-line verification passes can the API meet self-testing requirements. To make SDK development higher quality and more efficient, writing command-line code quickly and correctly is an urgent need in the development process. Summary of the Invention
[0003] This disclosure provides a method, apparatus, and computer-readable storage medium for generating command-line function code, which can automatically generate the code of multiple command-line functions within seconds based on the input information of the modeling file. This not only greatly improves development efficiency but also avoids errors that may occur when writing code manually.
[0004] One embodiment of this disclosure provides a method for generating command-line function code, including:
[0005] Obtain the modeling input file;
[0006] Parse the command line content in the modeling input file, the command line content including the function name corresponding to the command line, the full name of the command, and the description of the command;
[0007] Generate code for a command-line function based on the command-line content.
[0008] One embodiment of this disclosure also provides a command-line function code generation apparatus, including a processor and a memory storing a computer program, wherein the processor, when executing the computer program, can implement the command-line function code generation method as described in any embodiment of this disclosure.
[0009] An embodiment of this disclosure also provides a non-transient computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, is capable of implementing the command-line function code generation method as described in any embodiment of this disclosure.
[0010] Compared with related technologies, the command line function code generation method and apparatus provided in this disclosure adopt a modeling approach, extracting the modeling logic for generating code from the modeling input file as a generation program, and using the program that abstracts the command line generation logic to automatically generate the command line function code. Developers can focus on writing the command line itself. After filling in the modeling input file, the program is executed, and multiple command line function codes can be generated in seconds. This not only greatly improves development efficiency but also avoids errors that may occur in manual coding.
[0011] Other features and advantages of this disclosure will be set forth in the following description, and will be apparent in part from the description, or may be learned by practicing the disclosure. Other advantages of this disclosure may be realized and obtained by means of the methods described in the description and the accompanying drawings. Attached Figure Description
[0012] The accompanying drawings are used to provide an understanding of the technical solutions of this disclosure and form part of the specification. They are used together with the embodiments of this disclosure to explain the technical solutions of this disclosure and do not constitute a limitation on the technical solutions of this disclosure.
[0013] Figure 1 This is a flowchart of a command-line function code generation method according to an embodiment of this disclosure;
[0014] Figure 2 This is a schematic diagram of an exemplary data structure for storing mandatory options in a trie, according to one embodiment of this disclosure.
[0015] Figure 3 This is a flowchart of a command-line function generation method according to an embodiment of this disclosure;
[0016] Figure 4 This is a schematic diagram of a command-line function code generation apparatus according to an embodiment of the present disclosure. Detailed Implementation
[0017] This disclosure describes several embodiments, but these descriptions are exemplary and not limiting, and it will be apparent to those skilled in the art that many more embodiments and implementations are possible within the scope of the embodiments described herein. Although many possible combinations of features are shown in the drawings and discussed in the detailed description, many other combinations of the disclosed features are also possible. Unless specifically limited, any feature or element of any embodiment may be used in combination with, or may replace, any feature or element of any other embodiment.
[0018] This disclosure includes and contemplates combinations of features and elements known to those skilled in the art. The embodiments, features, and elements disclosed in this disclosure may also be combined with any conventional features or elements to form a unique inventive scheme as defined by the claims. Any feature or element of any embodiment may also be combined with features or elements from other inventive schemes to form another unique inventive scheme as defined by the claims. Therefore, it should be understood that any feature shown and / or discussed in this disclosure may be implemented individually or in any suitable combination. Therefore, the embodiments are not limited except by the limitations imposed by the appended claims and their equivalents. Furthermore, various modifications and changes may be made within the scope of the appended claims.
[0019] During SDK development, the software primarily provides APIs for customer use. Developing these APIs requires writing command-line code for API validation. Currently, this command-line code is written manually, which presents the following problems:
[0020] Manual coding is a huge workload. Many command lines have more lines of code than the API itself, and their number is comparable to the number of APIs (for example, there may be multiple command lines corresponding to one API function, or one command line corresponding to multiple API functions). Therefore, there will be repetitive work in the manual coding process, and a lot of effort is required.
[0021] This is prone to errors, especially when there are many command-line argument numbers and required options. The argument number is the position of the input argument within the command-line characters, separated by spaces. Regarding incorrect argument numbers, for example, it's easy to forget to update the corresponding API code after modifying the command line, leading to incorrect numbers and parsing errors; or when the required options are complex, manual counting is prone to miscounting, causing incorrect parameter option judgments.
[0022] When there are many required options, manually selecting the differences between different character options as the judgment condition can easily be confusing and lead to encoding errors.
[0023] It takes a long time and is inefficient. There is no reusability between different commands, and each new command needs to be rewritten, resulting in low development efficiency.
[0024] Therefore, one embodiment of this disclosure provides a method for generating command-line function code, such as... Figure 1 As shown, it includes the following steps:
[0025] Step S110: Obtain the modeling input file;
[0026] Step S120: Parse the command line content in the modeling input file. The command line content includes the function name corresponding to the command line, the full name of the command, and the description of the command.
[0027] Step S130: Generate the code for the command-line function based on the command-line content.
[0028] This embodiment employs a modeling approach, extracting the modeling logic from the modeling input file and writing it into an automatic code generation program. This program then generates the code for API-related command-line functions in SDK development with a single click. This automatic code generation method is simple, significantly improves development efficiency, and avoids errors that may occur with manual coding.
[0029] In an exemplary embodiment of this disclosure, generating the command-line function code based on the command-line content includes: generating the corresponding command-line function code for each command-line content in the modeling input file in the following manner:
[0030] Generate comments for command-line functions based on the command description;
[0031] Generate the function header of the command-line function based on the corresponding function name and the fully qualified name of the command.
[0032] The command field of the command-line function is generated based on the full name of the command.
[0033] In one example of this embodiment, the modeling input file is an Excel file, and Table 1 shows one of its command line contents:
[0034] Table 1
[0035]
[0036] Based on the command line content in Table 1, which includes comment information (command description), function header information (command line corresponding function name and command full name), and command field information (command full name), the command line function's comment, function header, and command field can be generated respectively.
[0037] In one example of this embodiment, generating a comment for a command-line function based on the command description includes: adding comment symbols to the command description as comment information for the command line; and adding the command line comment to the beginning of the command-line function.
[0038] In the example shown in Table 1, C language comment symbols are added to the beginning and end of the command description "prbs result reading command" to generate the command line function comment: / *prbs result reading command* / .
[0039] In one example of this embodiment, the function header sequentially includes a function declaration (extern function) and a command definition macro. The step of generating the command-line function header based on the command-line corresponding function name and the fully qualified name of the command includes:
[0040] Add a first suffix to the function name corresponding to the command line, which is used as a parameter representing the function in the extern function and the command definition macro; add a second suffix to the function name corresponding to the command line, which is used as a structure parameter in the command definition macro;
[0041] Generate the input and output parameters of the extern function based on the function name corresponding to the command line;
[0042] Based on the full name of the command, generate prompts for the current command line character and segment characters in the command definition macro.
[0043] In one example of this embodiment, the full name of a command consists of multiple characters separated by spaces. These characters include fixed characters and may also include non-fixed characters. Each character in the full name of the command can be extracted based on the spaces. The non-fixed characters include optional and required fields. For required fields, whether a character is required can be determined by its flag, such as whether it is enclosed in parentheses "()". Alternatively, the length of the entire command can be used to determine if an optional field is input. Furthermore, each optional and required field includes multiple segmented characters separated by delimiters (such as "|"). During the command field parsing and modeling process to generate code, fixed characters do not need to be parsed; these characters are distinguished from other commands by the command-line mechanism. Non-fixed characters need to be parsed. The relevant algorithms of the trie data structure can be used as the main algorithm logic for processing non-fixed characters (such as required fields and optional fields). Based on the delimiters in the non-fixed characters, the segment characters included in the non-fixed characters are extracted, and all segment characters are stored in a trie data structure. The parsing statement of different contents of the required fields is formed by traversing the trie, and the output is the code for parsing function fields.
[0044] The prompt information for the segmented characters is generated as follows: segmented characters are extracted from the non-fixed characters in the full name of the command; and, according to the order of the extracted segmented characters in the full name of the command, a newline character is added to each segmented character and written into the command definition macro as the prompt information for that segmented character.
[0045] In the example shown in Table 1, the code for the extern function in the generated function header is as follows:
[0046] extern XC_INT32 xc68_osu_olsdh_prbs_result_get_cmd(PCMDELEMENT self,scrn_t*xscrn,int argc,char**argv).
[0047] Here, "xc68_osu_olsdh_prbs_result_get_cmd" is the parameter representing the argument in the extern function of the general command parser, which adds the first suffix "cmd" to the corresponding function name in the command line. The parameters in parentheses are the input and output parameters generated based on the corresponding function name in the command line.
[0048] In the example shown in Table 1, the code for defining macros in the generated function header is as follows:
[0049]
[0050]
[0051] Among them, xc68_osu_olsdh_prbs_result_get_cmd represents the parameters of a function obtained by adding the first suffix "cmd" to the command-line function name; xc68_osu_olsdh_prbs_result_get_struct
[0052] It is the structure parameter in the command definition macro obtained by adding the second suffix "struct" to the command line function name; "show\n" is the prompt message for a segment character after adding a newline character to the segment character.
[0053] In one example of this embodiment, the command field for generating the command-line function based on the fully qualified name of the command includes:
[0054] When it is determined that the full name of the command contains non-fixed characters based on the flags of non-fixed characters, the non-fixed characters are parsed to generate parsing code for the command-line function. The non-fixed characters include optional fields and required fields.
[0055] The step of parsing the non-fixed characters to generate parsing code for command-line functions includes:
[0056] Based on the delimiter (e.g., "|") in the non-fixed characters, extract the segment characters included in the non-fixed characters, and store all segment characters in a trie-like data structure;
[0057] By traversing the trie and combining the position of the non-fixed character in the full name of the command, parsing code is generated for each segment character.
[0058] For example, in the process of processing the content of optional or required fields, it is determined whether the required or optional field contains multiple segment characters based on whether it contains "|". When it contains multiple segment characters, the required or optional field is processed by inserting the segment characters such as words separated by "|" into the trie using a trie. During parsing, the trie is traversed to obtain the parsing for each required or optional field.
[0059] In the example in Table 1, the fifth character in the fully qualified command name is a required field, including two segment characters "hp" and "lp". Therefore, according to the command-line function code generation method described in this example, the segmented command to be generated from the command line in Table 1 above is:
[0060]
[0061] In the code above, "[4]" in argv[4][0] means that the index of the character in the full name of the command is 4, and "[0]" means that the letter with index 0 in the character is the first letter. argv[4][0] can extract the letter with index 0 in the character with index 4 in the full name of the command.
[0062] The segmentation command described above can be used to parse the required fields in the command line entered during API verification. The required fields in the command line entered during API verification have only one value, namely a segment character. The above code can be used to parse this segment character.
[0063] In another example, assuming the fifth character of the fully qualified name of a command is a required field (ab|c|ac), the (ab|c|ac) delimiters are stored in a trie as follows: Figure 2 As shown, Figure 2 In the dictionary, 'a', 'b', and 'c' represent the lowercase letters 'a', 'b', and 'c' in the required fields. Then, traversing the dictionary yields the processing logic for the required fields. The processing for optional fields is similar and will not be elaborated further.
[0064] Assume (ab|c|ac) is a required option at position 4 in the fully qualified command name. Based on a depth-first traversal (i.e., comparing character positions first), generate the code for the command field of the following command-line function:
[0065]
[0066]
[0067] This example uses a trie data structure and related algorithms as the main algorithm logic. Spaces are used as delimiters to determine the position of non-fixed characters within the full name of the command. When a character is required, the segment characters included in the optional option are extracted using "|" and stored in a trie data structure. Then, the trie is traversed to obtain the option's judgment conditions, generating the code for the relevant command-line function. In other words, the parameter numbers and required option judgment conditions of the command-line function are generated using an algorithm, avoiding potential errors that might occur during manual coding.
[0068] The specific errors in the manual editing mentioned above may be as follows:
[0069] When the 9th character in the full name of the command is a required field "((jx_cfg_profile_sel|rx_c2_chk|rx_c2_value|etsl_vld|rx_k3_mode|rx_rei_blk|rx_bip_blk|force_ais))", the if conditional statement for "f" in "force_ais" should be if(argv[8][0]=='f'), where the ids of 8 and 0 are easy to miscount.
[0070] In an exemplary embodiment of this disclosure, after generating the code for the command-line function based on the command-line content, the method further includes: writing the code for the command-line function into a specified code file, and saving the code file according to the save path of the code file.
[0071] In one example of this embodiment, the code file is a C language file; the command-line function code is the command-line function code of the API in the SDK. However, this disclosure is not limited to this, and the command-line function code generation method of this embodiment can also be used to generate the code of other command-line functions.
[0072] One embodiment of this disclosure provides a method for generating command-line functions, which is used in the command-line function code generation method of the above embodiments of this disclosure, such as... Figure 3 As shown, the method for generating the command-line function includes the following steps:
[0073] Step S310, create the modeling Excel file, specifically as follows:
[0074] The command line to be written should be entered into a row of the specified Excel sheet according to the three parts of the modeling information (the function name corresponding to the command line, the full name of the command, and the description of the command). Table 2 shows the contents of the command line to be written.
[0075] Step S320: The program generates the cmd.c file, specifically as follows:
[0076] The program, which includes command-line generation logic, is executed. The command-line function code generation method described in any embodiment of this disclosure is used to parse each command line in the Excel file, generate the code for the command-line function corresponding to each command line, and write the generated command-line function code into a specified code file. In this example, the code file is saved as a C language code file named cmd.c.
[0077] Step S330: Supplement the API call part of the command-line function in the cmd.c file and assemble the API using a structure.
[0078] Using the example shown in Table 1, after generating the command-line function code from the command lines in Table 1, the code for the API call part and the code for assembling the API using a structure are added as follows:
[0079]
[0080]
[0081] The command line code to be written in Table 1, after being generated using the command line function code generation method shown in the above embodiments and with manual supplementation of API-related code, will ultimately generate the following complete code:
[0082]
[0083]
[0084] This embodiment uses an Excel document as the input file for modeling and employs a program that abstracts the command-line generation logic as the core parsing program for the modeling data. It extracts the logic for generating comments, function headers, prompts, and parameter checks from the command-line code, generating C language code for API-related command lines in SDK development with a single click and outputting it to the result file cmd.c. This allows developers to focus on writing the command lines themselves; they only need to fill in the command information in the Excel document and execute the program. Multiple command-line function C language codes can be generated within seconds, and the automatically generated code can be directly compiled. Developers only need to adapt the API for verification, which not only greatly improves development efficiency but also avoids errors that may occur during manual coding.
[0085] This disclosure also provides a command-line function code generation apparatus in one embodiment, see [link to relevant documentation]. Figure 4 The system includes a processor and a memory storing a computer program, wherein the processor, when executing the computer program, is capable of implementing the command-line function code generation method as described in any embodiment of this disclosure.
[0086] The processor in the embodiments of this disclosure can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), a microprocessor, etc., or other conventional processors. The processor can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA), discrete logic or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or other equivalent integrated or discrete logic circuits, or a combination of the above devices. That is, the processor in the embodiments described above can be any processing device or combination of devices that implements the methods, steps, and logic block diagrams disclosed in the embodiments of this invention. If the embodiments of this disclosure are implemented in part in software, then instructions for software can be stored in a suitable non-volatile computer-readable storage medium, and one or more processors can be used to execute the instructions in hardware to implement the methods of the embodiments of this disclosure. The term "processor" as used herein can refer to the above-described structure or any other structure suitable for implementing the techniques described herein.
[0087] An embodiment of this disclosure also provides a non-transient computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, is capable of implementing the command-line function code generation method as described in any embodiment of this disclosure.
[0088] In one or more exemplary embodiments described above, the described functionality may be implemented in hardware, software, firmware, or any combination thereof. If implemented in software, the functionality may be stored as one or more instructions or code on or transmitted via a computer-readable medium and executed by a hardware-based processing unit. The computer-readable medium may comprise a computer-readable storage medium corresponding to a tangible medium, such as a data storage medium, or a communication medium comprising any medium facilitating the transfer of a computer program from one place to another, such as according to a communication protocol. In this manner, the computer-readable medium may generally correspond to a non-transitory tangible computer-readable storage medium or a communication medium, such as a signal or carrier wave. The data storage medium may be any available medium accessible by one or more computers or one or more processors to retrieve instructions, code, and / or data structures for implementing the techniques described in this disclosure. Computer program products may comprise computer-readable media.
[0089] For example, and not as a limitation, such computer-readable storage media may include RAM, ROM, EEPROM, CD-ROM or other optical disc storage devices, magnetic disk storage devices or other magnetic storage devices, flash memory, or any other medium that can be used to store desired program code in the form of instructions or data structures and is accessible by a computer. Furthermore, any connection may also be referred to as a computer-readable medium. For example, if instructions are transmitted from a website, server, or other remote source using coaxial cable, fiber optic cable, twisted pair, digital subscriber line (DSL), or wireless technologies such as infrared, radio, and microwave, then coaxial cable, fiber optic cable, twisted pair, DSL, or wireless technologies such as infrared, radio, and microwave are included in the definition of medium. However, it should be understood that computer-readable storage media and data storage media do not include connections, carrier waves, signals, or other transient media, but rather refer to non-transient tangible storage media. As used herein, disks and optical discs include compact optical discs (CDs), laser discs, optical discs, digital versatile discs (DVDs), floppy disks, or Blu-ray discs, where disks typically reproduce data magnetically, while optical discs use lasers to reproduce data optically. The above combinations should also be included within the scope of computer-readable media.
Claims
1. A method for generating command-line function code, characterized in that, include: Obtain the modeling input file; Parse the command line content in the modeling input file. The command line content includes the function name corresponding to the command line, the full name of the command, and the description of the command. The full name of the command contains command field information. The code for generating a command-line function based on the command line content includes: for each command line content, generating the code for the corresponding command-line function in the following ways: generating a comment for the command-line function based on the command description; generating a function header for the command-line function based on the function name corresponding to the command line and the full name of the command; and generating a command field for the command-line function based on the full name of the command. The function header includes a declaration function and a command definition macro. Generating the function header of the command-line function based on the command-line corresponding function name and the fully qualified name of the command includes: adding a first suffix to the command-line corresponding function name as a parameter representing the function in the declaration function and the command definition macro; adding a second suffix to the command-line corresponding function name as a structure parameter in the command definition macro; generating the input and output parameters of the declaration function based on the command-line corresponding function name; and generating prompt information for the current command-line character and segment characters in the command definition macro based on the fully qualified name of the command. The step of generating the command field for the command-line function based on the full name of the command includes: when it is determined from the markers of non-fixed characters that the full name of the command contains non-fixed characters, parsing the non-fixed characters to generate parsing code for the command-line function, wherein the non-fixed characters include optional fields and required fields; parsing the non-fixed characters to generate parsing code for the command-line function includes: extracting segment characters included in the non-fixed characters based on the delimiters in the non-fixed characters, and storing all segment characters in a trie-like data structure; traversing the trie and combining the position of the non-fixed characters in the full name of the command to generate parsing code for each segment character.
2. The method according to claim 1, characterized in that, The step of generating comments for command-line functions based on the command description includes: Add comment symbols to the description of the command as comment information for the command line; Add a comment to the command line at the beginning of the command line function.
3. The method according to claim 1, characterized in that: The prompt information for the segmented characters is generated as follows: segmented characters are extracted from the non-fixed characters in the full name of the command; and, according to the order of the extracted segmented characters in the full name of the command, a newline character is added to each segmented character and written into the command definition macro as the prompt information for that segmented character.
4. The method according to claim 1, characterized in that: After generating the code for the command-line function based on the command-line content, the method further includes: Write the code of the command-line function into the specified code file, and save the code file according to the save path of the code file.
5. The method according to claim 4, characterized in that: The code file is a C language format file; The code for the command-line function is the code for the command-line function of the application programming interface in the software development kit.
6. A command-line function code generation apparatus, comprising a processor and a memory storing a computer program, wherein, When the processor executes the computer program, it can implement the command-line function code generation method as described in any one of claims 1 to 5.
7. A non-transient computer-readable storage medium storing a computer program, wherein, When the computer program is executed by a processor, it can implement the command-line function code generation method as described in any one of claims 1 to 5.