Sfoglia il codice sorgente

fixed default variable generation

xcad 4 mesi fa
parent
commit
93ca8bbbad
2 ha cambiato i file con 50 aggiunte e 10 eliminazioni
  1. 16 6
      cli/core/prompt.py
  2. 34 4
      cli/core/variables.py

+ 16 - 6
cli/core/prompt.py

@@ -91,16 +91,26 @@ class PromptHandler:
     return collected
 
   def _prompt_variable(self, variable: Variable, required: bool = False) -> Any:
-    """Prompt for a single variable value based on its type."""
+    """Prompt for a single variable value based on its type.
+    
+    Args:
+        variable: The variable to prompt for
+        required: Whether the containing section is required (for context/display)
+        
+    Returns:
+        The validated value entered by the user
+    """
     logger.debug(f"Prompting for variable '{variable.name}' (type: {variable.type})")
     
     # Use variable's native methods for prompt text and default value
     prompt_text = variable.get_prompt_text()
     default_value = variable.get_normalized_default()
 
-    # If variable is required and there's no default, mark it in the prompt
-    # (but skip this for autogenerated variables since they can be empty)
-    if required and default_value is None and not variable.autogenerated:
+    # Check if this specific variable is required (has no default and not autogenerated)
+    var_is_required = variable.is_required()
+    
+    # If variable is required, mark it in the prompt
+    if var_is_required:
       prompt_text = f"{prompt_text} [bold red]*required[/bold red]"
 
     handler = self._get_prompt_handler(variable)
@@ -121,8 +131,8 @@ class PromptHandler:
           return None  # Return None to indicate auto-generation should happen
         
         # If this variable is required, do not accept None/empty values
-        if required and (converted is None or (isinstance(converted, str) and converted == "")):
-          raise ValueError("value cannot be empty for required variable")
+        if var_is_required and (converted is None or (isinstance(converted, str) and converted == "")):
+          raise ValueError("This field is required and cannot be empty")
 
         # Return the converted value (caller will update variable.value)
         return converted

+ 34 - 4
cli/core/variables.py

@@ -313,6 +313,32 @@ class Variable:
     
     return " — ".join(hints) if hints else None
   
+  def is_required(self) -> bool:
+    """Check if this variable requires a value (cannot be empty/None).
+    
+    A variable is considered required if:
+    - It doesn't have a default value (value is None)
+    - It's not marked as autogenerated (which can be empty and generated later)
+    - It's not a boolean type (booleans default to False if not set)
+    
+    Returns:
+        True if the variable must have a non-empty value, False otherwise
+    """
+    # Autogenerated variables can be empty (will be generated later)
+    if self.autogenerated:
+      return False
+    
+    # Boolean variables always have a value (True or False)
+    if self.type == "bool":
+      return False
+    
+    # Variables with a default value are not required
+    if self.value is not None:
+      return False
+    
+    # No default value and not autogenerated = required
+    return True
+  
   def clone(self, update: Optional[Dict[str, Any]] = None) -> 'Variable':
     """Create a deep copy of the variable with optional field updates.
     
@@ -879,9 +905,10 @@ class VariableCollection:
             logger.debug(f"Skipping validation for autogenerated variable: '{section.key}.{var_name}'")
             continue
           
-          # If value is None, treat as missing
+          # If value is None and the variable is required, report as missing
           if variable.value is None:
-            errors.append(f"{section.key}.{var_name} (missing)")
+            if variable.is_required():
+              errors.append(f"{section.key}.{var_name} (required - no default provided)")
             continue
 
           # Attempt to convert/validate typed value
@@ -889,10 +916,13 @@ class VariableCollection:
 
           # For non-boolean types, treat None or empty string as invalid
           if variable.type not in ("bool",) and (typed is None or typed == ""):
-            errors.append(f"{section.key}.{var_name} (empty)")
+            if variable.is_required():
+              errors.append(f"{section.key}.{var_name} (required - cannot be empty)")
+            else:
+              errors.append(f"{section.key}.{var_name} (empty)")
 
         except ValueError as e:
-          errors.append(f"{section.key}.{var_name} (invalid: {e})")
+          errors.append(f"{section.key}.{var_name} (invalid format: {e})")
 
     if errors:
       error_msg = "Variable validation failed: " + ", ".join(errors)