File size: 4,199 Bytes
01d5a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
Cross-platform compatibility tool: Convert multi-line shell commands with backslash (\) line continuation to single-line commands
Usage: python convert_to_single_line.py input_file [output_file]
If no output file is specified, the input file will be overwritten
"""

import sys
import os
import re

def convert_multiline_to_single_line(file_path, output_path=None):
    """
    Convert multi-line commands (ending with backslash) to single-line commands
    """
    if output_path is None:
        output_path = file_path

    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        
        # Preserve the original file permissions
        original_mode = os.stat(file_path).st_mode
            
        # Use regular expression to find lines ending with \ and remove newlines and \
        pattern = r'\\\s*\n\s*'
        converted_content = re.sub(pattern, ' ', content)
            
        # Write the converted content to the output file
        with open(output_path, 'w', encoding='utf-8') as file:
            file.write(converted_content)
            
        # Restore the original file permissions
        os.chmod(output_path, original_mode)
        
        print(f"Successfully converted {file_path} to single-line command format, saved to {output_path}")
        return True
    except Exception as e:
        print(f"Error: {str(e)}")
        return False

def convert_single_line_to_multiline(file_path, output_path=None, line_prefix="--"):
    """
    Convert single-line commands back to multi-line format (using \ continuation), with each parameter on a separate line
    Mainly used to split parameters starting with line_prefix into separate lines
    """
    if output_path is None:
        output_path = file_path

    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        
        # Preserve the original file permissions
        original_mode = os.stat(file_path).st_mode
        
        # Find command lines and convert them
        lines = content.split('\n')
        converted_lines = []
        
        for line in lines:
            if line_prefix in line:
                # Split the command line
                parts = line.split(line_prefix)
                # Save the first part (command name)
                first_part = parts[0].rstrip() + " \\"
                converted_lines.append(first_part)
                
                # Process each parameter
                for i, part in enumerate(parts[1:], 1):
                    if part.strip():
                        # If not the last parameter, add continuation character
                        if i < len(parts) - 1:
                            converted_lines.append(line_prefix + part.rstrip() + " \\")
                        else:
                            converted_lines.append(line_prefix + part.rstrip())
            else:
                converted_lines.append(line)
        
        # Write the converted content to the output file
        with open(output_path, 'w', encoding='utf-8') as file:
            file.write('\n'.join(converted_lines))
        
        # Restore the original file permissions
        os.chmod(output_path, original_mode)
        
        print(f"Successfully converted {file_path} to multi-line command format, saved to {output_path}")
        return True
    except Exception as e:
        print(f"Error: {str(e)}")
        return False

def main():
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} input_file [output_file] [--to-multiline]")
        return
    
    file_path = sys.argv[1]
    output_path = sys.argv[2] if len(sys.argv) > 2 and not sys.argv[2].startswith('--') else None
    
    # Check if conversion to multi-line is needed
    to_multiline = '--to-multiline' in sys.argv
    
    if not os.path.exists(file_path):
        print(f"Error: File {file_path} does not exist")
        return
    
    if to_multiline:
        convert_single_line_to_multiline(file_path, output_path)
    else:
        convert_multiline_to_single_line(file_path, output_path)

if __name__ == "__main__":
    main()